[关闭]
@fiy-fish 2015-07-18T13:33:37.000000Z 字数 3898 阅读 1328

day03-03-NSString和NSMutableString

Objective-c


  1. // main.m
  2. // day03-03-NSString和NSMutableString
  3. //
  4. // Created by Aaron on 15/7/3.
  5. // Copyright (c) 2015年 Aaron. All rights reserved.
  6. //
  7. #import <Foundation/Foundation.h>
  8. //搜索字符串
  9. #if 0
  10. int main(int argc, const char * argv[]) {
  11. @autoreleasepool {
  12. NSString *str = @"abbcdeahbcfg";
  13. /*
  14. typedef struct _NSRange {
  15. NSUInteger location; 索引
  16. NSUInteger length; 长度
  17. } NSRange;
  18. */
  19. //rangeOfString: 搜索子字符串
  20. //返回一个range
  21. //注意: 返回的是第一次出现的范围
  22. NSRange range = [str rangeOfString:@"abc"];
  23. //判断是否搜索到的方法:
  24. //一:直接通过长度
  25. if(range.length != 0)
  26. {
  27. NSLog(@"搜索到");
  28. }
  29. else
  30. {
  31. NSLog(@"%ld %ld",range.location,range.length);
  32. }
  33. //二:利用位置
  34. if(range.location == NSNotFound)
  35. {
  36. NSLog(@"没有搜索到");
  37. }
  38. else
  39. {
  40. NSLog(@"搜索到了");
  41. }
  42. NSString *str2 = @"abcdefg";
  43. NSString *str3 = @"cde";
  44. NSRange range1 = {1,3};
  45. NSComparisonResult state = [str2 compare:str3 options:NSLiteralSearch range:range1];
  46. NSLog(@"%ld",state);
  47. }
  48. return 0;
  49. }
  50. #endif
  51. #if 0
  52. int main()
  53. {
  54. //抽取子字符串
  55. NSString *str = @"hello world";
  56. //NSRange range = {2,2};
  57. //按照范围来抽取子字符串
  58. //- (NSString *)substringWithRange:(NSRange)range;
  59. //str = [str substringWithRange:NSMakeRange(2, 2)];
  60. //NSLog(@"%@",str);
  61. //从指定的索引开始抽取子字符串
  62. //注意: 包含索引对应的字符
  63. //str = [str substringFromIndex:2];
  64. //NSLog(@"%@",str);
  65. //从头开始抽取到指定的索引为止
  66. //注意: 不包含索引对应的字符
  67. str = [str substringToIndex:2];
  68. NSLog(@"%@",str);
  69. }
  70. #endif
  71. /*
  72. When I was young, I'd listen to the radio, waiting for my favorite songs. 从was提取到radio;
  73. */
  74. #if 0
  75. int main()
  76. {
  77. NSString *str = @"When I was young, I'd listen to the radio, waiting for my favorite songs.";
  78. //1.确定用range来抽取
  79. //2.location是was里面w出现的位置
  80. NSRange range1 = [str rangeOfString:@"was"];
  81. //3.因为location可以代表前面有多少个字节
  82. NSRange range2 = [str rangeOfString:@"radio"];
  83. NSString *subStr = [str substringWithRange:NSMakeRange(range1.location, range2.location-range1.location+range2.length)];
  84. NSLog(@"%@",subStr);
  85. }
  86. #endif
  87. /*
  88. 查找子字符串出现的次数
  89. abcdefefefd
  90. ef
  91. */
  92. #if 0
  93. int main()
  94. {
  95. NSString *str = @"abcdefefefd";
  96. NSRange range = {0,0};
  97. NSInteger times = 0;
  98. while(1)
  99. {
  100. str = [str substringFromIndex:range.location+range.length];
  101. range = [str rangeOfString:@"ef"];
  102. if(range.location == NSNotFound)
  103. {
  104. break;
  105. }
  106. else
  107. {
  108. times++;
  109. }
  110. }
  111. NSLog(@"出现了%ld次",times);
  112. }
  113. #endif
  114. #if 0
  115. int main()
  116. {
  117. NSString *str = @"sgddshjagdhasgdh";
  118. //可以从后往前搜索
  119. NSRange range = [str rangeOfString:@"sgd" options:NSBackwardsSearch];
  120. //NSLog(@"%@",NSStringFromRange(range));
  121. //可以指定范围搜索
  122. //注意:返回的location是针对原字符串的索引
  123. range = [str rangeOfString:@"sgd" options:NSLiteralSearch range:NSMakeRange(1, 15)];
  124. // FOUNDATION_EXPORT NSString *NSStringFromRange(NSRange range);
  125. //这里的NSStringFromRange(range) 是一个字符串,也就是单纯的把range按照字符串的格式输出 可以输出range(结构体)的形式
  126. NSLog(@"%@",NSStringFromRange(range));
  127. }
  128. #endif
  129. #if 0
  130. int main()
  131. {
  132. NSString *str1 = @"hello world";
  133. //获取字符串的长度
  134. NSInteger length = [str1 length];
  135. NSLog(@"%ld",length);
  136. //- (unichar)characterAtIndex:(NSUInteger)index;
  137. //获取指定索引对应的字符
  138. //unichar 是转成unicode码之后的两个字节的数据
  139. NSLog(@"%C",[str1 characterAtIndex:0]);
  140. //将字符串转成大写
  141. str1 = [str1 uppercaseString];
  142. NSLog(@"%@",str1);
  143. //将字符串转成小写
  144. str1 = [str1 lowercaseString];
  145. NSLog(@"%@",str1);
  146. //将单词首字母转成大写
  147. str1 = [str1 capitalizedString];
  148. NSLog(@"%@",str1);
  149. //@"www.baidu.com"
  150. NSString *str2 = @"www.baidu.com";
  151. BOOL rec = [str2 hasPrefix:@"www"];
  152. NSLog(@"%@",rec?@"有前缀":@"无前缀");
  153. //作业:自己实现判断是否有前缀,后缀
  154. rec = [str2 hasSuffix:@".com"];
  155. NSLog(@"%@",rec?@"有后缀":@"无后缀");
  156. // /users/Aaron/desktop/test1.txt
  157. NSString *str3 = @"1.23";
  158. //将字符串转成基本类型的数据
  159. float temp = [str3 floatValue];
  160. NSLog(@"%.2f",temp);
  161. //注意:转换的时候,遇到不是数字的字符就停止
  162. NSString *str4 = @"a1001";
  163. int t = [str4 intValue];
  164. NSLog(@"%d",t);
  165. const char *s = [str4 UTF8String];
  166. NSLog(@"%s",s);
  167. }
  168. #endif
  169. int main()
  170. {
  171. //可变字符串
  172. NSMutableString *str1 = [[NSMutableString alloc] init];
  173. //init对于可变字符串是有意义的
  174. NSMutableString *str2 = [NSMutableString string];
  175. NSMutableString *str3 = [NSMutableString stringWithString:@"hello"];
  176. NSLog(@"%@",str3);
  177. NSString *str4 = [[NSString alloc] initWithString:str3];
  178. //可变字符串仅仅是在不可变的基础上增加了 增删改几种方法
  179. //增
  180. //追加字符串
  181. [str3 appendString:@"xxx"];
  182. NSLog(@"%@",str3);
  183. //追加格式
  184. [str3 appendFormat:@"teacherName:%@",@"xiaoxin"];
  185. NSLog(@"%@",str3);
  186. //插入
  187. [str3 insertString:@"..." atIndex:0];
  188. NSLog(@"%@",str3);
  189. //删除
  190. //删除指定范围内的字符
  191. [str3 deleteCharactersInRange:NSMakeRange(0, 8)];
  192. NSLog(@"%@",str3);
  193. //修改
  194. [str3 replaceCharactersInRange:NSMakeRange(3, 7) withString:@""];
  195. NSLog(@"%@",str3);
  196. //直接设置字符串内容
  197. [str3 setString:@""];
  198. NSLog(@"--%@",str3);
  199. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注