iOS NSString常用用法大全

一、NSRange

在对NSString介绍之前,我们先要了解一个结构体NSRange。

  1. typedef struct _NSRange  
  2. {  
  3.     unsignedint location;  
  4.   
  5.     unsignedint length;  
  6.   
  7. }NSRange;  


NSRange表示相关事物的一个范围,常与字符串操作一起使用。

Location时字段的起始位置,length为字段的长度。

例如:  

 

  1. NSRange range = [@"I am atany" rangeOfString:@"am"];//查找“am”字符串在“I am atany”中的位置。  
  2.   
  3. NSLog(@"%d,%d",range.location,range.length);//2,2  

 

声明一个NSRange有几种方法。

第一种:

  1. NSRange range;  
  2.   
  3. range.location = 0;  
  4.   
  5. range.length = 1;     

第二种:

  1. NSRange range = {0,1};  

第三种:

  1. NSRange range = NSMakeRange(0, 1);  


二、NSString

下面介绍一些NSString常用的方法。

1、字符串的声明

   1)直接赋值常量

  1. NSString *string1 = @"hello";  

   2)先分配内存初始化再赋值

 

  1. NSString *string2 = [[NSStringalloc]init];  
  2.   
  3. string2= @"hello";  

   3)使用工厂方法制造对象

  1. NSString *string3 = [NSStringstringWithFormat:@"hello"];  

   4)使用initWithStringinitWithFormat方法初始化

 

  1. NSString *string4 = [[NSStringalloc]initWithString:@"hello"];  
  2.   
  3. NSString *string4 = [[NSStringalloc]initWithFormat:@"hello"];  

   5)使用C字符串生成NSString

  1. NSString *string5 = [[NSStringalloc]initWithCString:"hello"encoding:NSUTF8StringEncoding];  

   6)读取本地文件里面的字符串,hello.text为相对路径,存放在项目中

 

  1. NSString *path = [[NSBundlemainBundle] pathForResource:@"hello.txt"ofType:nil];  
  2.   
  3. NSString *string6 = [[NSStringalloc] initWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];  

   7)使用已有的字符串生成

  1. NSString *string7 = [NSStringstringWithString:string6];  

 

2、获取字符串的长度

  1. NSString *str = [NSString stringWithFormat:@"first"];  
  2.   
  3. NSLog(@"str length is %d",str.length); //字符串长度  

 

3、转化字符串的大小写

  1. NSString *string = @"hEllO";  
  2.   
  3. NSLog(@"string is %@",[string uppercaseString]);//转化为大写  
  4.   
  5. NSLog(@"string is %@",[string lowercaseString]);//转化为小写  
  6.   
  7. NSLog(@"string is %@",[string capitalizedString]);//转化为首字母大写  

输出:

string is HELLO

string is hello

stringis Hello


4、判断字符串前缀与后缀

  1. NSString *hello = @"hello.png";//0代表匹配,1代表不匹配  
  2.   
  3. NSLog(@"hello has prefix %d",[hello hasPrefix:@"hello"]);//以hello开头  
  4.   
  5. NSLog(@"hello has suffix %d",[hello hasSuffix:@".png"]);//以.png结尾  

可以方便的实现判断文件类型等操作。


5、判断字符串是否相等

    如果相等,会返回YES,否则NO

  1. NSString *string1 = @"hello";  
  2.   
  3. NSString *string2 = [NSString stringWithFormat:@"hello"];  
  4.   
  5. NSString *string3 = @"hi";  
  6.   
  7. if([string1 isEqualToString:string2]){  
  8.     NSLog(@"string1 is Same to string2");  
  9. }  
  10. else{  
  11.     NSLog(@"string1 is different to string2");  
  12. }  
  13.   
  14. if([string1 isEqualToString:string3]){  
  15.     NSLog(@"string2 is Same to string3");  
  16. }  
  17. else{  
  18.     NSLog(@"string2 is different to string3");  
  19. }   

输出结果:

string1 is Same to string2

string2 is different to string3

 

6、包含其他字符串

    使用rangeOfString返回只一个NSRange参数,通过range的location与length可以方便的找到包含字符串

  1. NSString *string1 = @"hello";  
  2.   
  3. NSString *string2 = @"I am hello123";  
  4.   
  5. NSRange range = [string2 rangeOfString:string1];  
  6.   
  7. NSLog(@"range.location is %d,range.length is %d",range.location,range.length);  

输出:

range.location is 5,range.length is 5

如果不包含字符串,那么在range.location会等于NSNotFound


7、字符串的比较

字符串的比较使用 compare的方法

返回值:NSComparisonResult

,[@"hello" compare:@"hello"]);  

  •   
  • NSLog(@"flag is %d",[@"hello" compare:@"aello"]);  
  •   
  • NSLog(@"flag is %d",[@"hello" compare:@"hello1"]);  
  •   
  • NSLog(@"flag is %d",[@"hello" compare:@"Hello"]);//h在H之后  

结果为:

flag is 0

flag is 1

flag is -1

flag is 1


8、不区分大小写的比较

使用compareoptionsoptions参数是一个掩位码,可以用“|”符号使用多个。

NSCaseInsensitiveSearch:不区分大小写

NSLiteralSearch:区分大小写

NSNumericSearch:比较字符串的个数,而不是子字符值

  1. NSLog(@"%d",[@"hello" compare:@"Hello" options:NSCaseInsensitiveSearch]);//返回值为0  
  2.   
  3. NSLog(@"%d",[@"hello" compare:@"Hello" options: NSLiteralSearch]);//返回值为1  
  4.   
  5. NSLog(@"%d",[@"Name7.txt" compare:@"Name25.txt" options:NSNumericSearch]); //返回值为-1  
  6.   
  7. NSLog(@"%d",[@"Name7.txt" compare:@"Name25.txt"]);//返回值为1  

 

9、截取字符串

  1. NSString *string = @"hello world";  
  2.   
  3. NSString *string2 = [string substringToIndex:2];//2为长度(从0开始截取)  
  4.   
  5. NSLog(@"string2 is %@",string2);  
  6.   
  7. NSString *string3 = [string substringFromIndex:2];//从2开始(截取到最后)  
  8.   
  9. NSLog(@"string3 is %@",string3);  
  10.   
  11. NSRange range = { 2,2 };//从2开始截取,截取2位  
  12.   
  13. NSString *string4 = [string substringWithRange:range];  
  14.   
  15. NSLog(@"string4 is %@",string4);  

输出:

string2 is he

string3 is llo world

string4 is ll


10、去除字符串首尾的空格

  1. NSString *text = [@"     hello     " stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];  
  2.   
  3. NSLog(text);//hello  

 

11、NSMutableString

NSString创建之后不可变的,而NSMutableString为可变的,类似与java中的string与stringBuffer。

NSMutableString为NSString子类,继承所有方法。

1)创建字符串

 50这个值只是一个参考,不够的话系统会自动补充。

  1. NSMutableString *string = [NSMutableStringstringWithCapacity:50];  

2)在字符串尾添加新的字符串

  1. [string appendString:@"hi"];  
  2.   
  3. NSLog(@"string is %@",string);//string is hi  

3)使用Format形式添加新的字符串

  1. [string appendFormat:@" I am %@",@"atany"];  
  2.   
  3. NSLog(@"string is %@",string);//string is hi I am atany  

4)删除字符串

  1. NSRange range = [string rangeOfString:@"am"];//找回”am”字符串位置(5,2)  
  2.   
  3. [string deleteCharactersInRange:range];//先用rangeOfString来得到range  
  4.   
  5. NSLog(@"string is %@",string);// string is hi I  atany  

5)在字符串中插入字符串

  1. [string insertString:@"York"atIndex:1];//插入到h之后  
  2.   
  3. NSLog(@"string is %@",string);// string is hYorki I  atany  
  4.   
  5. [string setString:@"replace"];//改变字符串  
  6.   
  7. NSLog(@"string is %@",string);// string is replace  
  8.   
  9. [string replaceCharactersInRange:range withString:@"is"];  
  10.   
  11. NSLog(@"string is %@",string);// string is replais  

注:range只是记录的一个字符串位置,而不是字符串,此时range为上次rangeOfString:@"am"的位置(52)。


                                                                         **************************************************************

                                                                              atany原创,转载请注明博主与博文链接,3Q吐舌头

                                                                  http://blog.csdn.net/yang8456211/article/details/11744505

                                                                                                                                —— by atany

                                                                        **************************************************************

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值