1、ObjectC不可变字符串Object-C中的字符串对象1-不可变字符串#importintmain(intargc,constcharchar*argv)autoreleasepoolNSString*str1=thisisstringA;NSString*str2=thisisstringB;NSString*temp;NSComparisonResultcompareResult;/01.计算字符串中字符个数NSLog(字符串长度:%的长度为%lu,str1,str1length);/02.将一个字符串复制到另一字符串temp=NSStringstringWithString:str1;
2、NSLog(字符串复制:%,temp);/03.将一个字符串复制到另一字符串末尾temp=str1stringByAppendingString:str2;NSLog(字符串拼接:%,temp);/04.验证两个字符串是否相等if(str1isEqualToString:str2)NSLog(字符串比较:%与%相同,str1,str2);elseNSLog(字符串比较:%与%不相同,str1,str2);/05.验证一个字符串是否小于,等于或大于另一字符串compareResult=str1compare:str2;if(compareResult=NSOrderedAscending)NSL
3、og(字符串比较:%,str1,str2);/06.将字符串转为大写temp=str1uppercaseString;NSLog(字符串大写:%,temp);/07.将字符串转为小写temp=str1lowercaseString;NSLog(字符串小写:%,temp);/08.从字符串中取前3个字符temp=str1substringToIndex:3;NSLog(字符串截取:%,temp);/09.从字符串中截取第5个字符以后的字符temp=str1substringFromIndex:5;NSLog(字符串截取:%,temp);/10.截取从索引5到索引13的子字符temp=str1su
4、bstringFromIndex:5substringToIndex:8;NSLog(字符串截取:%,temp);temp=str1substringWithRange:NSMakeRange(5,8);NSLog(字符串截取:%,temp);/11.从另一字符串查找一个字符串NSString*subStr=stringA;NSRangesubRange=str1rangeOfString:subStr;NSLog(字符串查找:%位于%第:%lu个字符,长度为:%lu,subStr,str1,subRange.location,subRange.length);subRange=str2ran
5、geOfString:subStr;if(subRange.location=NSNotFound)NSLog(字符串查找:%中不存在%,str2,subStr);elseNSLog(字符串查找:%中存在%,str2,subStr);return0;输出结果:2014-02-12 22:41:43.890 05.字符串1560:303字符串长度:this is string A的长度为162014-02-12 22:41:43.892 05.字符串1560:303字符串复制:this is string A2014-02-12 22:41:43.892 05.字符串1560:303字符串拼接:
6、this is string Athis is string B2014-02-12 22:41:43.892 05.字符串1560:303字符串比较:this is string A与this is string B不相同2014-02-12 22:41:43.893 05.字符串1560:303字符串比较:this is string Athis is string B2014-02-12 22:41:43.893 05.字符串1560:303字符串大写:THIS IS STRING A2014-02-12 22:41:43.893 05.字符串1560:303字符串小写:this is
7、string a2014-02-12 22:41:43.893 05.字符串1560:303字符串截取:thi2014-02-12 22:41:43.894 05.字符串1560:303字符串截取:is string A2014-02-12 22:41:43.894 05.字符串1560:303字符串截取:is strin2014-02-12 22:41:43.894 05.字符串1560:303字符串截取:is strin2014-02-12 22:41:43.895 05.字符串1560:303字符串查找:string A位于this is string A第:8个字符,长度为:82014
8、-02-12 22:41:43.895 05.字符串1560:303字符串查找:this is string B中不存在string AProgram ended with exit code: 0Object-C中的数字对象1. #import2. 3. intmain(intargc,constcharchar*argv)4. 5. 6. autoreleasepool7. 8. /*9. Object-C中的数字对象:10. 1.int,float,long,double等都是Object-C中的基本数据类型,不是对象,也就是说不能够向他们发送消息。11. 2.有时需要将值类型作为对象使
9、用,如NSArray,要求存储的必须是对象,因此不能直接将基本数据类型放入数组。12. 3.如果需要存储基本数据类型(包括char),可以使用NSNumber类。13. */14. 15. NSNumber*intNumber,*longNumber,*floatNumber,*charNumber,*doubleNumber;16. 17. /int型值18. intNumber=NSNumbernumberWithInt:100;19. NSLog(%i,intNumberintValue);20. 21. /long型值22. longNumber=NSNumbernumberWithL
10、ong:0xabcdef;23. NSLog(%lx,longNumberlongValue);24. 25. /char型值26. charNumber=NSNumbernumberWithChar:A;27. NSLog(%c,charNumbercharValue);28. 29. /float型值30. floatNumber=NSNumbernumberWithFloat:100.00;31. NSLog(%g,floatNumberfloatValue);32. 33. /double型值34. doubleNumber=NSNumbernumberWithDouble:12345
11、e+15;35. NSLog(%lg,doubleNumberdoubleValue);36. 37. /验证两个值是否相等38. if(intNumberisEqualToNumber:floatNumber=YES)39. 40. NSLog(%i和%g比较,值相等,intNumberintValue,floatNumberfloatValue);41. 42. else43. 44. NSLog(%i和%g比较,值不等,intNumberintValue,floatNumberfloatValue);45. 46. 47. /两个值比较大小48. if(intNumbercompare:
12、floatNumber=NSOrderedAscending)49. 50. NSLog(%i和%g比较,第一个数第二个数,intNumberintValue,floatNumberfloatValue);51. 52. else53. 54. NSLog(%i和%g比较,第一个数第二个数,intNumberintValue,floatNumberfloatValue);55. 56. 57. return0;58. 输出结果:2014-02-12 21:59:34.847 04.数字1229:303 1002014-02-12 21:59:34.849 04.数字1229:303 abcde
13、f2014-02-12 21:59:34.849 04.数字1229:303 A2014-02-12 21:59:34.850 04.数字1229:303 1002014-02-12 21:59:34.850 04.数字1229:303 1.2345e+192014-02-12 21:59:34.850 04.数字1229:303 100和100比较,值相等2014-02-12 21:59:34.851 04.数字1229:303 100和100比较,第一个数第二个数Program ended with exit code: 0Object-C中的字符串对象2-可变字符串1. /2. /mai
14、n.m3. /字符串-可变字符串4. /5. /Createdbyzhangqs008on14-2-13.6. /Copyright(c)2014年zhangqs008.Allrightsreserved.7. /8. 9. #import10. 11. intmain(intargc,constcharchar*argv)12. 13. 14. autoreleasepool15. 16. NSString*str1=thisisstringA;17. NSString*search;18. NSString*replace;19. NSMutableString*mutableStr;20
15、. NSRangesubStr;21. 22. /01.从不可变字符串创建可变字符串23. mutableStr=NSMutableStringstringWithString:str1;24. NSLog(01%,mutableStr);25. 26. /02.插入字符,注意:是直接修改的字符串对象,无返回值27. mutableStrinsertString:mutableatIndex:7;28. NSLog(02%,mutableStr);29. 30. /03.插入末尾进行有效拼接31. mutableStrinsertString:andstringBatIndex:mutable
16、Strlength;32. NSLog(03%,mutableStr);33. 34. /04.直接用appendString35. mutableStrappendString:andstringC;36. NSLog(04%,mutableStr);37. 38. /05.根据范围删除字符串deleteCharactersInRange(从*开始,删除*个字符)39. mutableStrdeleteCharactersInRange:NSMakeRange(16,13);40. NSLog(05%,mutableStr);41. 42. /06.查找然后将其删除43. subStr=mu
17、tableStrrangeOfString:stringBand;44. if(subStr.location!=NSNotFound)45. 46. mutableStrsubstringWithRange:subStr;47. NSLog(06%,mutableStr);48. 49. 50. 51. /07.直接设置为可变字符串52. mutableStrsetString:thisisstringA;53. NSLog(07%,mutableStr);54. 55. /08.替换一些字符56. mutableStrreplaceCharactersInRange:NSMakeRange
18、(8,8)withString:amutablestring;57. NSLog(08%,mutableStr);58. 59. /09.查找和替换60. search=thisis;61. replace=Anexampleof;62. 63. subStr=mutableStrrangeOfString:search;64. if(subStr.location!=NSNotFound)65. 66. mutableStrreplaceCharactersInRange:subStrwithString:replace;67. NSLog(09%,mutableStr);68. 69. 7
19、0. /10.查找和替换所有匹配项71. search=a;72. replace=X;73. subStr=mutableStrrangeOfString:search;74. while(subStr.location!=NSNotFound)75. mutableStrreplaceCharactersInRange:subStrwithString:replace;76. subStr=mutableStrrangeOfString:search;77. 78. NSLog(10%,mutableStr);79. 80. /也可以这样写81. mutableStrreplaceOccu
20、rrencesOfString:searchwithString:replaceoptions:nilrange:NSMakeRange(0,mutableStrlength);82. 83. NSLog(10%,mutableStr);84. 85. 86. 87. 88. return0;89. 输出结果:2014-02-13 20:38:30.074 05.字符串-可变字符串997:303 01 this is string A2014-02-13 20:38:30.076 05.字符串-可变字符串997:303 02 this is mutable string A2014-02-13
21、 20:38:30.079 05.字符串-可变字符串997:303 03 this is mutable string A and string B2014-02-13 20:38:30.079 05.字符串-可变字符串997:303 04 this is mutable string A and string B and string C2014-02-13 20:38:30.080 05.字符串-可变字符串997:303 05 this is mutable string B and string C2014-02-13 20:38:30.080 05.字符串-可变字符串997:303 06 this is mutable string B and string C2014-02-13 20:38:30.081 05.字符串-可变字符串997:303 07 this is string A2014-02-13 20:38:30.081 05.字符串-可变字符串997:303 08 this is a mutable string2014-02-13 20:38:30.081 05.字符串-可变字符串997:303 09 An example of a mutable string2014-02-13 20:38:30.082 05.字符串-可变字符串
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1