星空网 > 软件开发 > 操作系统

IOS阶段学习第14天笔记(NSString与NSMutableString)

IOS学习(OC语言)知识点整理

一、OC字符串的操作

1)OC中字符串分为两种:

   1、不可变字符串NSString:不能修改对象内容,但是可以改变对象的指针。

   2、可变字符串NSMutableString:可以修改对象内容。

 

二、NSString 不可变字符串的操作

1)将字符串常量对象直接赋值给字符串引用 NSString *str1=@"hello"; 字符串对象的输出格式:NSLog(@"str1=%@",str1)。

 

2)initWithString可将OC中的字符串对象构建字符串引用  NSString *str2=[[NSString alloc]initWithString:str1]; 。

 

3)initWithUTF8String可将C语言的字符串创建OC的字符串对象,将C字符串转换为OC字符串:

     NSString *str3=[[NSString alloc]initWithUTF8String:"iOS"];

 

4)initWithFormat可将OC的格式化字符串创建OC的字符串对象int age=20; 

     NSString *str4=[[NSString alloc]initWithFormat:@"name is %@,age is  %d",str1,age];

 

5)可使用.length方法获取字符串的长度 NSUInteger len= str1.length;

 

6)characterAtIndex可根据下标取出某个字符 如: NSString *str1=@"hello"; unichar c= [str1 characterAtIndex:0];

    结果为:h

 

7)compare用于比较两个字符串,该方法区分大小写, 返回结果为NSComparisonResult 枚举类型数据 枚举值有

    1、NSOrderedAscending 表示前一个字符串小于后一个字符串

    2、NSOrderedSame 表示两个字符串相等

    3、NSOrderedDescending 表示前一个字符串大于后一个字符串

实例代码:

 1 NSString *str1=@"hello"; 2 NSString *str3=[[NSString alloc]initWithUTF8String:"iOS"]; 3 NSComparisonResult cmp=[str1 compare:str3]; 4 if(cmp==NSOrderedAscending){ 5  NSLog(@"%@<%@",str1,str3); 6 }else if (cmp==NSOrderedSame){ 7  NSLog(@"%@=%@",str1,str3); 8 }else if (cmp==NSOrderedDescending){ 9  NSLog(@"%@>%@",str1,str3);10 }//结果:hello<iOS

 

8)caseInsensitiveCompare 不区分大小写比较字符串;比较字符串,可以设置比较选项

    NSNumericSearch:如果字符串中有数字, 按数字大小比较,例如:ios5<iso12

    NSCaseInsensitiveSearch:不区分大小写比较,例如iOS7=ios7

 实例代码:

 1 NSComparisonResult cmp=[str1 caseInsensitiveCompare:str3]; 2 str1=@"iOS7"; 3 str3=@"ios7"; 4 cmp=[str1 compare:str3 options:NSCaseInsensitiveSearch]; 5 if(cmp==NSOrderedAscending){ 6 NSLog(@"%@<%@",str1,str3); 7 }else if (cmp==NSOrderedSame){ 8  NSLog(@"%@=%@",str1,str3); 9 }else if (cmp==NSOrderedDescending){10  NSLog(@"%@>%@",str1,str3);11  } 结果:iOS7=ios7

 

9)isEqualToString 比较2个字符串内容是否相等,返回BOOL(YES/NO)值

实例代码:

1 NSString *str1=@"hello";2 NSString *str2=[[NSString alloc]initWithString:str1];3 if([str1 isEqualToString:str2]){4   NSLog(@"%@ equal %@",str1,str2);5 }//结果:hello equal hello

10)uppercaseString 将小写字母字符串转为大写   NSString *str1=@"hello";NSString *str6=[str1 uppercaseString];

      结果为:HELLO 

 

 11)lowercaseString 将大写字母字符串转为小写 。

 

 12)hasPrefix 判断是否以某个字符串开头(作为前缀)例如 :

        NSString  str1=@"www.baidu.com";

        if([str1 hasPrefix:@"www"]){

            NSLog(@"yes");

        }  结果:yes

 

13)hasSuffix 判断是否以某个字符串结尾(后缀)例如:

    NSString  str1=@"www.baidu.com";

    if([str1 hasSuffix:@"com"]){

            NSLog(@"yes");

     }结果:yes

 

14)substringFromIndex 从某个下标开始取子字符串(到结束)例如:

        NSString  str1=@"This is string A";

        NSString *res=[str1 substringFromIndex:1]; 结果:his is string A

 

15)substringToIndex 从第一个字符开始取到某个下标的子字符串,不包括当前数字对应的字符,取当前数字下标前面字符

      例如:

        NSString  str1=@"This is string A";

        NSString  *res=[str1 substringToIndex:2];结果:Th

        [[str1 substringFromIndex:8] substringToIndex:5];结果: strin

 

16)substringWithRange 根据范围返回子字符串

        NSRange range={8,5};//直接给范围值

        range=NSMakeRange(8, 5);//通过函数返回一个范围的变量

        NSString  str1=@"This is string A";

        NSString res=[str1 substringWithRange:range]; 结果:strin

 

 17)rangeOfString 在字符串中查找子字符串,返回这个字符串的所在位置以及长度 NSRange

实例代码:

1 NSString str1=@"This is string A";2 NSRange range=[str1 rangeOfString:@"string"]; 结果:{8,6}3  if(range.location!=NSNotFound){4      NSLog(@"find");5   }//结果:find

 

18)使用 #if 0  代码段… #endif 可以让系统跳过某段代码不执行,例如:
        #if 0  NSLog(@“hello world!”); #endif  NSLog(@“我是第二段代码!”);
        执行结果:我是第二段代码!

19)intValue、longValue、floatValue、doubleValue 可将字符串类型的数字转换为数值类型的数字
       例如:
         NSString *str=@“88”;
         int a=[str intValue];


20)NSString 多个字符串的拼接

实例代码:

1  NSString *str1=@"My name is";2  NSString *str2=@"John Zhang";3  NSString *str3=@"I am 45";4  NSString *s1=[NSString stringWithFormat:@"%@ %@ %@",str1,str2,str3];5  NSLog(@"s1=%@",s1); //结果:s1=My name is John Zhang I am 45

 

21)initWithContentsOfFile  将文件内容读取成字符串对象, 第一个参数是文件名(绝对路径),第二个参数是编码类型,
      第三个参数是错误回调信息  例如:
      NSString *content=[[NSString alloc]initWithContentsOfFile:/Documents/ocfile/Person.h"
     encoding:NSUTF8StringEncoding error:nil];


22)直接打印对象,默认调用description方法,显示对象的类名和地址,可以重写description方法,
        注意:如果直接输出类对象中含有中文内容会有乱码,所以含中文内容的类对象必须遍历输出。

 三、NSMutableString 可变字符串的操作

 1)initWithString 用不可变字符串创建可变字符串对象 例如:

      NSString *str=@"This is string B";

      NSMutableString *mStr=[[NSMutableString alloc]initWithString:str];

 

 2)insertString 在指定下标位置插入一个字符串 例如:

      NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];

      [mStr insertString:@"hello " atIndex:0]; 结果:hello This is string B

 

3)appendString 在字符串后面追加字符串 例如:

     NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];

     [mStr appendString:@" shanghai”];结果:This is string B shanghai

 

4)appendFormat 在字符串后面追加一个格式化字符串 例如:

     NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];

     [mStr appendFormat:@" %d",20];结果:This is string B 20

 

5)deleteCharactersInRange 将指定范围的字符串删除 例如:

     NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];

     [mStr deleteCharactersInRange:NSMakeRange(0, 6)]; 结果: s string B

 

6)replaceCharactersInRange 将指定范围的字符串用新的字符串替换 例如:

1 NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];2 [mStr replaceCharactersInRange:NSMakeRange(2, 2) withString:@"IS"]; //结果:ThIS is string B3 //将字符串中所有的is替换为IS4 NSRange range=[mStr rangeOfString:@"is"];5 while (range.location!=NSNotFound) {6    [mStr replaceCharactersInRange:range withString:@"IS"];7    range=[mStr rangeOfString:@"is"];8 }

 

7)replaceOccurrencesOfString 将字符串中指定范围内所有的is替换成IS 例如:

    NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];

    [mStr replaceOccurrencesOfString:@“is” withString:@“IS” options:1 range:NSMakeRange(0, mStr.length)]; 

    结果:ThIS IS string B

 

8)setString 修改字符串内容 例如: NSMutableString *mStr=[[NSMutableString alloc]initWithString:@"This is string B"];

    [mStr setString:@"hello"]; 结果:hello

 

9)NSMutableString 实现多个字符串的拼接  
实例代码:

1 NSString *str1=@"My name is";2 NSString *str2=@"John Zhang";3 NSString *str3=@"I am 45";4 NSMutableString *s2=[[NSMutableString alloc]initWithString:str1]; 5 6 [s2 appendString:str2];7 [s2 appendString:@" "];8 [s2 appendString:str3];9 NSLog(@"s2=%@",s2);  //结果:s2=My name isJohn Zhang I am 45




原标题:IOS阶段学习第14天笔记(NSString与NSMutableString)

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

德国促销网Mydealz:https://www.ikjzd.com/w/223
快飞鱼QuickFish:https://www.ikjzd.com/w/2230
俄优选:https://www.ikjzd.com/w/2231
mail.ru:https://www.ikjzd.com/w/2232
pandao:https://www.ikjzd.com/w/2233
Beru.ru:https://www.ikjzd.com/w/2234
字节跳动辟谣!TikTok收紧美国开店政策为不实信息:https://www.kjdsnews.com/a/1836537.html
字节跳动辟谣!TikTok收紧美国开店政策为不实信息:https://www.goluckyvip.com/news/188212.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流