ObjectC不可变字符串.docx
- 文档编号:10490665
- 上传时间:2023-02-13
- 格式:DOCX
- 页数:17
- 大小:19.57KB
ObjectC不可变字符串.docx
《ObjectC不可变字符串.docx》由会员分享,可在线阅读,更多相关《ObjectC不可变字符串.docx(17页珍藏版)》请在冰豆网上搜索。
ObjectC不可变字符串
Object-C中的字符串对象1-不可变字符串
#import
int main(int argc, const charchar * argv[])
{
@autoreleasepool {
NSString *str1=@"this is string A";
NSString *str2=@"this is string B";
NSString *temp;
NSComparisonResult compareResult;
//01.计算字符串中字符个数
NSLog(@"字符串长度:
%@的长度为%lu",str1,[str1 length]);
//02.将一个字符串复制到另一字符串
temp=[NSString stringWithString:
str1];
NSLog(@"字符串复制:
%@",temp);
//03.将一个字符串复制到另一字符串末尾
temp=[str1 stringByAppendingString:
str2];
NSLog(@"字符串拼接:
%@",temp);
//04.验证两个字符串是否相等
if([str1 isEqualToString:
str2])
{
NSLog(@"字符串比较:
%@与%@相同",str1,str2);
}
else
{
NSLog(@"字符串比较:
%@与%@不相同",str1,str2);
}
//05.验证一个字符串是否小于,等于或大于另一字符串
compareResult=[str1 compare:
str2];
if(compareResult==NSOrderedAscending){NSLog(@"字符串比较:
%@<%@",str1,str2);}
else if(compareResult==NSOrderedSame){NSLog(@"字符串比较:
%@=%@",str1,str2);}
else{NSLog(@"字符串比较:
%@>%@",str1,str2);}
//06.将字符串转为大写
temp=[str1 uppercaseString];
NSLog(@"字符串大写:
%@",temp);
//07.将字符串转为小写
temp=[str1 lowercaseString];
NSLog(@"字符串小写:
%@",temp);
//08.从字符串中取前3个字符
temp=[str1 substringToIndex:
3];
NSLog(@"字符串截取:
%@",temp);
//09.从字符串中截取第5个字符以后的字符
temp=[str1 substringFromIndex:
5];
NSLog(@"字符串截取:
%@",temp);
//10.截取从索引5到索引13的子字符
temp=[[str1 substringFromIndex:
5]substringToIndex:
8];
NSLog(@"字符串截取:
%@",temp);
temp=[str1 substringWithRange:
NSMakeRange(5, 8)];
NSLog(@"字符串截取:
%@",temp);
//11.从另一字符串查找一个字符串
NSString *subStr=@"string A";
NSRange subRange=[str1 rangeOfString:
subStr];
NSLog(@"字符串查找:
%@位于%@第:
%lu个字符,长度为:
%lu",subStr,str1,subRange. location,subRange .length);
subRange=[str2 rangeOfString:
subStr];
if(subRange.location==NSNotFound)
{
NSLog(@"字符串查找:
%@中不存在%@",str2,subStr);
}
else
{
NSLog(@"字符串查找:
%@中存在%@",str2,subStr);
}
}
return 0;
}
输出结果:
2014-02-1222:
41:
43.89005.字符串[1560:
303]字符串长度:
thisisstringA的长度为16
2014-02-1222:
41:
43.89205.字符串[1560:
303]字符串复制:
thisisstringA
2014-02-1222:
41:
43.89205.字符串[1560:
303]字符串拼接:
thisisstringAthisisstringB
2014-02-1222:
41:
43.89205.字符串[1560:
303]字符串比较:
thisisstringA与thisisstringB不相同
2014-02-1222:
41:
43.89305.字符串[1560:
303]字符串比较:
thisisstringA 2014-02-1222: 41: 43.89305.字符串[1560: 303]字符串大写: THISISSTRINGA 2014-02-1222: 41: 43.89305.字符串[1560: 303]字符串小写: thisisstringa 2014-02-1222: 41: 43.89305.字符串[1560: 303]字符串截取: thi 2014-02-1222: 41: 43.89405.字符串[1560: 303]字符串截取: isstringA 2014-02-1222: 41: 43.89405.字符串[1560: 303]字符串截取: isstrin 2014-02-1222: 41: 43.89405.字符串[1560: 303]字符串截取: isstrin 2014-02-1222: 41: 43.89505.字符串[1560: 303]字符串查找: stringA位于thisisstringA第: 8个字符,长度为: 8 2014-02-1222: 41: 43.89505.字符串[1560: 303]字符串查找: thisisstringB中不存在stringA Programendedwithexitcode: 0 Object-C中的数字对象 1.#import 2. 3.int main(int argc, const charchar * argv[]) 4.{ 5. 6. @autoreleasepool { 7. 8. /* 9. Object-C中的数字对象: 10. 1.int,float,long,double等都是Object-C中的基本数据类型,不是对象,也就是说不能够向他们发送消息。 11. 2.有时需要将值类型作为对象使用,如NSArray,要求存储的必须是对象,因此不能直接将基本数据类型放入数组。 12. 3.如果需要存储基本数据类型(包括char),可以使用NSNumber类。 13. */ 14. 15. NSNumber *intNumber,*longNumber,*floatNumber,*charNumber,*doubleNumber; 16. 17. //int型值 18. intNumber=[NSNumber numberWithInt: 100]; 19. NSLog(@"%i",[intNumber intValue]); 20. 21. //long型值 22. longNumber=[NSNumber numberWithLong: 0xabcdef]; 23. NSLog(@"%lx",[longNumber longValue]); 24. 25. //char型值 26. charNumber=[NSNumber numberWithChar: 'A']; 27. NSLog(@"%c",[charNumber charValue]); 28. 29. //float型值 30. floatNumber=[NSNumber numberWithFloat: 100.00]; 31. NSLog(@"%g",[floatNumber floatValue]); 32. 33. //double型值 34. doubleNumber=[NSNumber numberWithDouble: 12345e+15]; 35. NSLog(@"%lg",[doubleNumber doubleValue]); 36. 37. //验证两个值是否相等 38. if([intNumber isEqualToNumber: floatNumber]==YES) 39. { 40. NSLog(@"%i和%g比较,值相等",[intNumber intValue],[floatNumber floatValue]); 41. } 42. else 43. { 44. NSLog(@"%i和%g比较,值不等",[intNumber intValue],[floatNumber floatValue]); 45. } 46. 47. //两个值比较大小 48. if([intNumber compare: floatNumber]==NSOrderedAscending) 49. { 50. NSLog(@"%i和%g比较,第一个数<第二个数",[intNumber intValue],[floatNumber floatValue]); 51. } 52. else 53. { 54. NSLog(@"%i和%g比较,第一个数≥第二个数",[intNumber intValue],[floatNumber floatValue]); 55. } 56. } 57. return 0; 58.} 输出结果: 2014-02-1221: 59: 34.84704.数字[1229: 303]100 2014-02-1221: 59: 34.84904.数字[1229: 303]abcdef 2014-02-1221: 59: 34.84904.数字[1229: 303]A 2014-02-1221: 59: 34.85004.数字[1229: 303]100 2014-02-1221: 59: 34.85004.数字[1229: 303]1.2345e+19 2014-02-1221: 59: 34.85004.数字[1229: 303]100和100比较,值相等 2014-02-1221: 59: 34.85104.数字[1229: 303]100和100比较,第一个数≥第二个数 Programendedwithexitcode: 0 Object-C中的字符串对象2-可变字符串 1.// 2.// main.m 3.// 字符串-可变字符串 4.// 5.// Created by zhangqs008 on 14-2-13. 6.// Copyright (c) 2014年 zhangqs008. All rights reserved. 7.// 8. 9.#import 10. 11.int main(int argc, const charchar * argv[]) 12.{ 13. 14. @autoreleasepool { 15. 16. NSString *str1=@"this is string A"; 17. NSString *search; 18. NSString *replace; 19. NSMutableString *mutableStr; 20. NSRange subStr; 21. 22. //01.从不可变字符串创建可变字符串 23. mutableStr=[NSMutableString stringWithString: str1]; 24. NSLog(@"01 %@",mutableStr); 25. 26. //02.插入字符,注意: 是直接修改的字符串对象,无返回值 27. [mutableStr insertString: @" mutable" atIndex: 7]; 28. NSLog(@"02 %@",mutableStr); 29. 30. //03.插入末尾进行有效拼接 31. [mutableStr insertString: @" and string B" atIndex: [mutableStr length]]; 32. NSLog(@"03 %@",mutableStr); 33. 34. //04.直接用appendString 35. [mutableStr appendString: @" and string C"]; 36. NSLog(@"04 %@",mutableStr); 37. 38. //05.根据范围删除字符串 deleteCharactersInRange(从*开始,删除*个字符) 39. [mutableStr deleteCharactersInRange: NSMakeRange(16,13)]; 40. NSLog(@"05 %@",mutableStr); 41. 42. //06.查找然后将其删除 43. subStr=[mutableStr rangeOfString: @"string B and "]; 44. if(subStr.location! =NSNotFound) 45. { 46. [mutableStr substringWithRange: subStr]; 47. NSLog(@"06 %@",mutableStr); 48. 49. } 50. 51. //07.直接设置为可变字符串 52. [mutableStr setString: @"this is string A"]; 53. NSLog(@"07 %@",mutableStr); 54. 55. //08.替换一些字符 56. [mutableStr replaceCharactersInRange: NSMakeRange(8, 8) withString: @"a mutable string"]; 57. NSLog(@"08 %@",mutableStr); 58. 59. //09.查找和替换 60. search=@"this is"; 61. replace=@"An example of"; 62. 63. subStr=[mutableStr rangeOfString: search]; 64. if(subStr.location! =NSNotFound) 65. { 66. [mutableStr replaceCharactersInRange: subStr withString: replace]; 67. NSLog(@"09 %@",mutableStr); 68. } 69. 70. //10.查找和替换所有匹配项 71. search=@"a"; 72. replace=@"X"; 73. subStr=[mutableStr rangeOfString: search]; 74. while (subStr.location! =NSNotFound) { 75. [mutableStr replaceCharactersInRange: subStr withString: replace]; 76. subStr=[mutableStr rangeOfString: search]; 77. } 78. NSLog(@"10 %@",mutableStr); 79. 80. //也可以这样写 81. [mutableStr replaceOccurrencesOfString: search withString: replace options: nil range: NSMakeRange(0, [mutableStr length])]; 82. 83. NSLog(@"10 %@",mutableStr); 84. 85. 86. 87. } 88. return 0; 89.} 输出结果: 2014-02-1320: 38: 30.07405.字符串-可变字符串[997: 303]01thisisstringA 2014-02-1320: 38: 30.07605.字符串-可变字符串[997: 303]02thisismutablestringA 2014-02-1320: 38: 30.07905.字符串-可变字符串[997: 303]03thisismutablestringAandstringB 2014-02-1320: 38: 30.07905.字符串-可变字符串[997: 303]04thisismutablestringAandstringBandstringC 2014-02-1320: 38: 30.08005.字符串-可变字符串[997: 303]05thisismutablestringBandstringC 2014-02-1320: 38: 30.08005.字符串-可变字符串[997: 303]06thisismutablestringBandstringC 2014-02-1320: 38: 30.08105.字符串-可变字符串[997: 303]07thisisstringA 2014-02-1320: 38: 30.08105.字符串-可变字符串[997: 303]08thisisamutablestring 2014-02-1320: 38: 30.08105.字符串-可变字符串[997: 303]09Anexampleofamutablestring 2014-02-1320: 38: 30.08205.字符串-可变字符串
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ObjectC 不可 字符串
![提示](https://static.bdocx.com/images/bang_tan.gif)