Foundation框架的一些实用方法:替换字符串,去空格,反转

 1 //定义一个可变字符串, Format后面可以跟字符串类型,也可以传入C语言的字符串数组
 2 NSMutableString *str = [NSMutableString stringWithFormat:@"<#NSString#>"];
 3 
 4 // 获取NSMutableString或NString 某位置的字符
 5 NSMutableString *newS1=[NSMutableString stringWithString:s1];
 6 for (int i = 0; i<s1.length; i++) {
 7     char c1 = [newS1 characterAtIndex:i]; // 用c1去接收了newS1 i 位置的字符
 8 }
 9 
10 //初始化一个NSArray
11 NSMutableArray *arr = [NSMutableArray array];
12 //给数组arr增加元素
13 [arr addObject:xxx]
14 
15 //定义一个字典
16 NSMutableDictionary *dict1 = [NSMutableDictionary dictionaryWithDictionary:@{<#@"k1":@"v1",@"k2":@"v2"#>}];
17 
18 //  替换字符串
19 NSMutableString *str = [NSMutableString stringWithFormat:@"123iositheimaios"];
20 NSString *newStr =  [str stringByReplacingOccurrencesOfString:@"ios" withString:@"andriod"];
21 
22 // 去空格
23 +(NSString *)removeSpace:(NSString *)s{
24     NSString *newStr = [s stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
25     return newStr;
26 }
27 
28 // 字符串反转 
29 unsigned long len = str.length;
30 NSMutableString *newStr;
31 for (unsigned long i=len; i>0; i--) {
32     [newStr appendString:[str  substringWithRange:NSMakeRange(i-1, 1)]];
33 }
34 return newStr;

 

posted on 2016-03-27 11:14  _MR.Q  阅读(290)  评论(0)    收藏  举报

导航