温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

【Objective-C】OC中字符串(NSString)的基本概念和常用处理方法

发布时间:2020-05-26 16:35:07 来源:网络 阅读:494 作者:jiangqq900826 栏目:移动开发

在Foundation框架中,提供了NNString类,用于处理字符串对象。在C语言中字符串在一对引号中表示。

OC是C语言的一种延展,换句话说,OC语言是建立在C语言的基础之上(C的基础之上添加了面向对象的内容,smalltalk语言)。

因此,为了区别两者的字符串,OC语言的字符串必须以@开头,引号当中则是字符串的内容,如@”abc“.此外,NSString对象,

一旦被创建,就不可以在进行修改。如若需要一个可以更改的字符串对象,需要创建NSMutableString实例。

 1:下面是一些创建字符串的方法:


        //创建一个字符串常量

        NSString *string1=@"这个一个字符串常量";         //创建一个空的字符串         NSString *string2=[[NSString alloc]init];         NSString *string3=[NSString string];         //快速创建一个字符串         NSString *string4=[[NSString alloc]initWithString:@"快速创建一个字符串"];         NSString *string5=[NSString stringWithString:@"快速创建一个字符串"];         //快速创建一个格式化字符串         int number=2;         NSString *string6=[[NSString alloc]initWithFormat:@"%d",number];         NSString *string7=[NSString stringWithFormat:@"%d",number];


2:比较字符串的内容是否相同


   

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(@"相同内容");         }else{             NSLog(@"不同内容");         }


3:比较两个字符串是否为同一个对象

  

 

//比较两个字符串是否是同一个对象         NSString *string8=[NSString stringWithString:@"123"];         NSString *string9=[NSString stringWithString:@"123"];         if([string8 isEqualToString:string9]){             NSLog(@"相同内容");         }else{             NSLog(@"不同内容");         }         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:创建字符串还有如下一些常用的方法:



- (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:(const char *)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:(const void *)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:(const char *)nullTerminatedCString; + (id)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); + (id)localizedStringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);  - (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding; + (id)stringWithCString:(const char *)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:字符串的转换

    

 //字符串的转换

        

NSString *string13=[[NSString alloc]initWithString:@"HelloWorld"];         NSLog(@"upper %@",[string13 uppercaseString]);         NSLog(@"lower %@",[string13 lowercaseString]);         NSLog(@"capitalized %@",[string13 capitalizedString]);
8:字符串转换成基本数据类型
      
 //字符串转成基本数据类型         NSString *string14=[[NSString alloc]initWithString:@"3.14"];         NSLog(@"%.2f\n",[string14 floatValue]); 
9:字符串转换成数组
       
 //字符串转换成数组         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!");         }

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI