iOS字符串的常用方法

NSString* str=@"hello";//存在代码区,不可变

 NSLog(@"%@",str);

  1.【字符串插入】

    NSMutableString* str1=[[NSMutableString alloc]initWithString:@"hello"];//存在堆区,可变字符串

    NSLog(@"str1:%@",str1);

    [str1 insertString:@"234"atIndex:2];//把一个字符串插入另一个字符串中的某一个位置

    NSLog(@"str1:%@",str1);

  2.【字符串覆盖】

   NSRange range={1,2};//字符串覆盖另一个字符串(覆盖范围可以设定)

   [str1 replaceCharactersInRange:rangewithString:@"dffdf"];

   NSLog(@"str1:%@",str1);

  3.【字符串截取】

       NSMutableString* str4=[[NSMutableString alloc]initWithString:@"handaiy"];

       NSString* str5=[str4 substringFromIndex:5];//截取从第5位到最后一位

       NSLog(@"截取后的字符串为:%@",str5);

       NSMutableString* str6=[[NSMutableString alloc]initWithString:@"handaiy"];

       NSString* str7=[str6 substringToIndex:5];//截取从0位到第4位

       NSLog(@"截取后的字符串为:%@",str7);

       NSMutableString* str8=[[NSMutableString alloc]initWithString:@"handaiy"];

       NSRange ange={2,3};

       NSString* str9=[str8 substringWithRange:ange];//截取从第二位到第4位

       NSLog(@"截取后的字符串为:%@",str9);       

 4.【字符串的大小转换】

(1)将所有的字符串内容变为大写字母  uppercaseString

(2)将所有的字符串内容变为小写字母  lowercaseString

(3)将单词的首字母小写变为大写字母  capitalizedString

       NSString* str10=@"sayhelloworld";

       NSString* str11=[str10uppercaseString];

        NSLog(@"str11:%@",str11);//(1)将所有的字符串内容变为大写字母

       <------------------------------------------------------------------------------------------>

       NSString* str12=@"Hello WORD";

       NSString* str13=[str12 lowercaseString];

        NSLog(@"str11:%@",str13);//(2)将所有的字符串内容变为小写字母

        NSString* stu=@"sayhelloworld";

       NSString* aa=[stu substringToIndex:3];//截取say

       NSString* stu1=[aa capitalizedString];//把say首字母变成大写

       NSRange rang={3,5};//截取hello

       NSString* bb=[stu substringWithRange:rang];//取出hello创建对象bb

       NSString* stu2=[bb capitalizedString];//把hello首字母变成大写

       NSString* dd=[stu substringFromIndex:8];

       NSString* stu3=[dd capitalizedString];

     <------------------------------------------------------------------------------------------>

        //拼接方法(1)

       NSString* stu4=[NSStringstringWithFormat:@"%@%@%@",stu1,stu2,stu3];//把截取出来的单词拼接成新的字符串。

        NSLog(@"截取单词后,每个单词首字母变成大写,然后重新拼接成的新字符串:%@",stu4);//(4)字符串截取、改变、拼接

        //拼接方法(2)如:

       NSString* h=@"2014年";

       NSString* j=@"9月";

       NSString* k=@"26号";

       NSString* riqi=[[[[h stringByAppendingString:@"_"]

                            stringByAppendingString:j]

                            stringByAppendingString:@"_"]

                            stringByAppendingString:k];

       NSLog(@"%@",riqi);

 5.【分割字符串】

       NSString* nba=@"2014.9.26";

       NSArray *p=[nba componentsSeparatedByString:@"."];

       NSLog(@"分割后的字符串:%@",p);        

6.【字符串转化为数字型再计算】intValue、floatVale...

       NSString* cba=@"2014";

       int s=[cba intValue];

       int d=s+1;

       NSLog(@"字符串转化成数字后再计算:%d",d);

  7.【数字型转化为字符串】

        int z=45534;

        NSString* g=[NSStringstringWithFormat:@"%d",z];

        NSLog(@"数字转化为字符串:%@",g);

 8.字符串的末尾追加新的字符串

       NSString *bbc =@"www.cn";

       NSString *bbc1 = [bbc stringByAppendingString:@"itcast.cn"];

       NSLog(@"bbc1: %@",bbc1);

9.在制定的范围追加字符串(插入字符串、替换字符串)

      NSString *aac =@"wwwbbbccc";

      NSRange ranges = {4,0};

      NSString *aac1 = [aac stringByReplacingCharactersInRange:rangeswithString:@"aa"];

      NSLog(@"aac1: %@",aac1);        

10.使用新的字符串替换原有的字符串或删除指定的字符串

       NSString *cca =@"ww w.bai.com";

        NSString *cca1 = [cca stringByReplacingOccurrencesOfString:@"bai"withString:@"du"];//替换

        NSString *cca2 = [cca stringByReplacingOccurrencesOfString:@"co"withString:@""];//删除

        NSString *cca3 = [cca stringByReplacingOccurrencesOfString:@" "withString:@""];//删除空格号

        NSLog(@"cca1: %@ cca2: %@ cca3: %@",cca1,cca2,cca3);

 11.带引号的字符串


       NSString *string = @"hello, \"world\"";

       NSLog(@"带引号的字符串:%@",string);

       NSString *string2 = [NSString stringWithFormat:@"hello,\"%@\"",@"世界"];

       NSLog(@"拼接字符串:%@",string2);

你可能感兴趣的:(ios)