在Foundation框架中,提供了NNString類,用于處理字符串對象。在C語言中字符串在一對引號中表示。
成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)浮山,十年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220
OC是C語言的一種延展,換句話說,OC語言是建立在C語言的基礎(chǔ)之上(C的基礎(chǔ)之上添加了面向?qū)ο蟮膬?nèi)容,smalltalk語言)。
因此,為了區(qū)別兩者的字符串,OC語言的字符串必須以@開頭,引號當(dāng)中則是字符串的內(nèi)容,如@”abc“.此外,NSString對象,
一旦被創(chuàng)建,就不可以在進(jìn)行修改。如若需要一個可以更改的字符串對象,需要創(chuàng)建NSMutableString實(shí)例。
1:下面是一些創(chuàng)建字符串的方法:
//創(chuàng)建一個字符串常量
NSString*string1=@"這個一個字符串常量";//創(chuàng)建一個空的字符串NSString*string2=[[NSString alloc]init];NSString*string3=[NSString string];//快速創(chuàng)建一個字符串NSString*string4=[[NSString alloc]initWithString:@"快速創(chuàng)建一個字符串"];NSString*string5=[NSString stringWithString:@"快速創(chuàng)建一個字符串"];//快速創(chuàng)建一個格式化字符串int number=2;NSString*string6=[[NSString alloc]initWithFormat:@"%d",number];NSString*string7=[NSString stringWithFormat:@"%d",number];
NSString*string6=[[NSString alloc]initWithFormat:@"%d",number];NSString*string7=[NSString stringWithFormat:@"%d",number];NSLog(@"%@,%@,%@,%@,%@,%@,%@",string1,string2,string3,string4,string5,string6,string7);//比較兩個字符串是否相等if([string6 isEqualToString:string7]){ NSLog(@"相同內(nèi)容");}else{ NSLog(@"不同內(nèi)容");}
3:比較兩個字符串是否為同一個對象
//比較兩個字符串是否是同一個對象NSString*string8=[NSString stringWithString:@"123"];NSString*string9=[NSString stringWithString:@"123"];if([string8 isEqualToString:string9]){ NSLog(@"相同內(nèi)容");}else{ NSLog(@"不同內(nèi)容");}if(string8==string9){ NSLog(@"同一個對象");}else{ NSLog(@"不同對象");}
4:比較字符串的大小
//比較字符串的大小NSString*string10=[[NSString alloc]initWithString:@"a"];NSString*string11=[[NSString alloc]initWithString:@"b"];NSComparisonResult result=[string10 caseInsensitiveCompare:string11];NSLog(@"%ld",result);//-1表示升序
5:創(chuàng)建字符串還有如下一些常用的方法:
-(id)init;-(id)initWithCharactersNoCopy:(unichar*)characters length:(NSUInteger)length freeWhenDone:(BOOL)freeBuffer;/* "NoCopy" is a hint */-(id)initWithCharacters:(const unichar*)characters length:(NSUInteger)length;-(id)initWithUTF8String:(constchar*)nullTerminatedCString;-(id)initWithString:(NSString*)aString;-(id)initWithFormat:(NSString*)format,... NS_FORMAT_FUNCTION(1,2);-(id)initWithFormat:(NSString*)format arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);-(id)initWithFormat:(NSString*)format locale:(id)locale,... NS_FORMAT_FUNCTION(1,3);-(id)initWithFormat:(NSString*)format locale:(id)locale arguments:(va_list)argList NS_FORMAT_FUNCTION(1,0);-(id)initWithData:(NSData*)data encoding:(NSStringEncoding)encoding;-(id)initWithBytes:(constvoid*)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding;-(id)initWithBytesNoCopy:(void*)bytes length:(NSUInteger)len encoding:(NSStringEncoding)encoding freeWhenDone:(BOOL)freeBuffer;/* "NoCopy" is a hint */+(id)string;+(id)stringWithString:(NSString*)string;+(id)stringWithCharacters:(const unichar*)characters length:(NSUInteger)length;+(id)stringWithUTF8String:(constchar*)nullTerminatedCString;+(id)stringWithFormat:(NSString*)format,... NS_FORMAT_FUNCTION(1,2);+(id)localizedStringWithFormat:(NSString*)format,... NS_FORMAT_FUNCTION(1,2);-(id)initWithCString:(constchar*)nullTerminatedCString encoding:(NSStringEncoding)encoding;+(id)stringWithCString:(constchar*)cString encoding:(NSStringEncoding)enc;/* These use the specified encoding. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors). */-(id)initWithContentsOfURL:(NSURL*)url encoding:(NSStringEncoding)enc error:(NSError**)error;-(id)initWithContentsOfFile:(NSString*)path encoding:(NSStringEncoding)enc error:(NSError**)error;+(id)stringWithContentsOfURL:(NSURL*)url encoding:(NSStringEncoding)enc error:(NSError**)error;+(id)stringWithContentsOfFile:(NSString*)path encoding:(NSStringEncoding)enc error:(NSError**)error;/* These try to determine the encoding, and return the encoding which was used. Note that these methods might get "smarter" in subsequent releases of the system, and use additional techniques for recognizing encodings. If nil is returned, the optional error return indicates problem that was encountered (for instance, file system or encoding errors). */-(id)initWithContentsOfURL:(NSURL*)url usedEncoding:(NSStringEncoding*)enc error:(NSError**)error;-(id)initWithContentsOfFile:(NSString*)path usedEncoding:(NSStringEncoding*)enc error:(NSError**)error;+(id)stringWithContentsOfURL:(NSURL*)url usedEncoding:(NSStringEncoding*)enc error:(NSError**)error;+(id)stringWithContentsOfFile:(NSString*)path usedEncoding:(NSStringEncoding*)enc error:(NSError**)error;6:求字符串的長度
//求字符串的長度NSString*string12=[[NSString alloc]initWithString:@"abcd"];NSLog(@"string2的長度為:%ld",[string12 length]);
7:字符串的轉(zhuǎn)換
//字符串的轉(zhuǎn)換
NSString*string13=[[NSString alloc]initWithString:@"HelloWorld"];NSLog(@"upper %@",[string13 uppercaseString]);NSLog(@"lower %@",[string13 lowercaseString]);NSLog(@"capitalized %@",[string13 capitalizedString]);
8:字符串轉(zhuǎn)換成基本數(shù)據(jù)類型
//字符串轉(zhuǎn)成基本數(shù)據(jù)類型NSString*string14=[[NSString alloc]initWithString:@"3.14"];NSLog(@"%.2f\n",[string14floatValue]);9:字符串轉(zhuǎn)換成數(shù)組
//字符串轉(zhuǎn)換成數(shù)組NSString*string15=[[NSString alloc]initWithString:@"abc bcd xyz"];NSArray*array=[string15 componentsSeparatedByString:@""];NSLog(@"%@\n",array);10:字符串的截取
//截取字符串NSString*string16=@"abcdefg";NSLog(@"%@",[string16 substringToIndex:2]);NSLog(@"%@",[string16 substringFromIndex:2]);NSRange range; range.location=2; range.length=3;NSLog(@"%@",[string16 substringWithRange:range]);11:字符串的拼接://字符串的拼接NSString*string17=@"abc";NSString*string18=@"xyz";NSString*appString=[[NSString alloc]initWithFormat:@"這是拼接的字符串:%@ and %@",string17,string18];NSString*appString2=[string17 stringByAppendingString:@"123"];NSString*appstring3=[string17 stringByAppendingFormat:@"%@",string18]; NSLog(@"%@,%@,%@",appString,appString2,appstring3);12:字符串的查找://字符串的查詢NSString*link=@"abcdffe-===fefjfwfw";NSRange range1=[link rangeOfString:@"abcd"];NSLog(@"%@",NSStringFromRange(range1));if(range1.location!=NSNotFound){ NSLog(@"found!");}
網(wǎng)頁名稱:【Objective-C】OC中字符串(NSString)的基本概念和常用處理方法
標(biāo)題鏈接:http://www.rwnh.cn/article24/jcjgce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營銷型網(wǎng)站建設(shè)、網(wǎng)站策劃、自適應(yīng)網(wǎng)站、網(wǎng)站設(shè)計公司、微信小程序、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)