天天看点

iOS字符串的各种用法(字符串插入、字符串覆盖、字符串截取、分割字符串)

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

       nslog(@"%@",str);

        //1.【字符串插入】

        nsmutablestring* str1=[[nsmutablestringalloc]initwithstring:@"hello"];//存在堆区,可变字符串

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

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

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

        //2.【字符串覆盖】

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

        [str1 replacecharactersinrange:rangewithstring:@"dffdf"];

        //3.【字符串截取】

        nsmutablestring* str4=[[nsmutablestringalloc]initwithstring:@"handaiy"];

       nsstring* str5=[str4substringfromindex:5];//截取从第5位到最后一位

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

        nsmutablestring* str6=[[nsmutablestringalloc]initwithstring:@"handaiy"];

       nsstring* str7=[str6substringtoindex:5];//截取从0位到第4位

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

        nsmutablestring* str8=[[nsmutablestringalloc]initwithstring:@"handaiy"];

       nsrange ange={2,3};

       nsstring* str9=[str8substringwithrange: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=[str12lowercasestring];

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

        nsstring* stu=@"sayhelloworld";

       nsstring* aa=[stusubstringtoindex:3];//截取say

       nsstring* stu1=[aacapitalizedstring];//把say首字母变成大写

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

       nsstring* bb=[stusubstringwithrange:rang];//取出hello创建对象bb

       nsstring* stu2=[bbcapitalizedstring];//把hello首字母变成大写

       nsstring* dd=[stusubstringfromindex:8];

       nsstring* stu3=[ddcapitalizedstring];

        //拼接方法(1)

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

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

        //拼接方法(2)如:

       nsstring* h=@"2014年";

       nsstring* j=@"9月";

       nsstring* k=@"26号";

       nsstring* riqi=[[[[hstringbyappendingstring:@"_"]

                            stringbyappendingstring:j]

                            stringbyappendingstring:@"_"]

                            stringbyappendingstring:k];

       nslog(@"%@",riqi);

        //5.【分割字符串】

       nsstring* nba=@"2014.9.26";

        nsarray *p=[nbacomponentsseparatedbystring:@"."];

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

        //6.【字符串转化为数字型再计算】intvalue、floatvale...

       nsstring* cba=@"2014";

       int s=[cbaintvalue];

       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 = [bbcstringbyappendingstring:@"itcast.cn"];

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

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

       nsstring *aac =@"wwwbbbccc";

       nsrange ranges = {4,0};

        nsstring *aac1 = [aacstringbyreplacingcharactersinrange:rangeswithstring:@"aa"];

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

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

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

        nsstring *cca1 = [ccastringbyreplacingoccurrencesofstring:@"bai"withstring:@"du"];//替换

        nsstring *cca2 = [ccastringbyreplacingoccurrencesofstring:@"co"withstring:@""];//删除

        nsstring *cca3 = [ccastringbyreplacingoccurrencesofstring:@" "withstring:@""];//删除空格号

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

                   //11.带引号的字符串

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

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

       nsstring *string2 = [nsstring stringwithformat:@"hello,\"%@\"",@"世界"];

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