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);