一、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)值
实例代码:
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

OC之字符串 NSString与NSMutableString的更多相关文章

  1. Objective-C: 字符串NSString与NSMutableString

    字符串算是OC中非常重要和常用的一部分内容,OC中的字符串与我之前在学习C,C++,Java中的字符串有一定的不同,它非常类似于C++中容器的概念,但用法却与之还是有很大的不同,也许是因为OC的语法就 ...

  2. OC第二节 —— NSString和NSMutableString

    1.为什么需要NSString对象        答:在OC中创建字符串时,一般不使用C的方法,    因为C将字符串作为字符数组,所以在操作时会有很多不方便的地方,    在Cocoa中NSStri ...

  3. 14.Object-C--浅谈Foundation框架字符串NSString 与NSMutableString

    OC的字符串时经常使用到的,今天我对于OC字符串做一个简单的总结,如果有错误之处,麻烦留言指正.感谢! NSString是一个不可变长度的字符串对象.表示它初始化以后,你不能改变该变量所分配的内存中的 ...

  4. 字符串NSString与NSMutableString常用方法

    NSString 1.初始化 NSString *str1 = @"a OC Program"; 2.初始化 NSString *str2 = [[NSString alloc] ...

  5. OC字符串NSString

    ========================== 面向对象编程进阶和字符串 ========================== Δ一.类的设计模式—单例 [单例]程序允许过程中,有且仅有一块内存 ...

  6. OC本学习笔记Foundation框架NSString与NSMutableString

       一.NSString与NSMutableString         相信大家对NSString类都不陌生.它是OC中提供的字符串类.它的对象中的字符串都是不可变的,而它的子类NSMutable ...

  7. OC中Foundation框架之NSString、NSMutableString

    创建方式 )直接赋值 NSString *str =@"abc"; )创建对象 NSString *str2 = [[NSString alloc]init]; str2 =@&q ...

  8. OC之NSString、NSMutableString学习笔记 常用方法

    NSString篇: 1.字符串连接 NSString *beijing = @"北京"; NSString *welcome = [beijing stringByAppendi ...

  9. OC——NSString和NSMutableString

    int main(int argc, const char * argv[]) { @autoreleasepool { //----------------NSString------------- ...

随机推荐

  1. ppt怎么换背景图片|PPT换背景设置方法

    PPT怎么换背景?PPT背景可谓是PPT幻灯片的灵魂,优美绚丽的PPT背景能为自己做的PPT幻灯片锦上添花.今天,格子啦小编就教大家PPT换背景的方法,让你不再羡慕别人所做PPT的美丽背景,也可以自己 ...

  2. C# windows窗体程序打包安装及卸载

    一.新建安装部署项目

  3. XCode7打包上传报错

      在XCode7上传应用时,上传失败遇到两个错误,提示如下: ERROR ITMS-90535: "Unexpected CFBundleExecutable Key. The bundl ...

  4. codevs 1243 网络提速

    题目描述 Description 某学校的校园网由n(1<=n<=50)台计算机组成,计算机之间由网线相连,如图5.其中顶点代表计算机,边代表网线.正如你所见,不同网线的传输能力不尽相同, ...

  5. codevs 1153 道路游戏

    传送门   题目描述 Description 小新正在玩一个简单的电脑游戏.游戏中有一条环形马路,马路上有n 个机器人工厂,两个相邻机器人工厂之间由一小段马路连接.小新以某个机器人工厂为起点,按顺时针 ...

  6. PostBack与IsPostBack区别

    这涉及到aspx的页面回传机制的基础知识 postback是回传 即页面在首次加载后向服务器提交数据,然后服务器把处理好的数据传递到客户端并显示出来,就叫postback, ispostback只是一 ...

  7. How Many Points of Intersection?

    uva10790:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  8. C51 库函数(2)

    3.2 STDIO.H:一般I/O函数 C51编译器包含字符I/O函数,它们通过处理器的串行接口操作,为支持其它I/O机制,只需修改getkey()和putchar()函数,其它所有I/O支持函数依赖 ...

  9. form 转json最佳示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. 14.4.3 Adaptive Hash Index 自适应hash index

    14.4.3 Adaptive Hash Index 自适应hash index 自适应hash index(AHI) 让InnoDB 执行更像内存数据库在系统使用合适的负载组合和足够的内存用于Buf ...