foundation 框架 NSString常用总结(二)

以此作为NSString常用总结(一)的补充

 1      NSString* str = [NSString stringWithFormat:@"I love programing! You, come here"];

 2         //字符串对象的大小写转换

 3         //转换为大写

 4         NSString* upStr = [str uppercaseString];

 5         NSLog(@"str = %@", str);

 6         NSLog(@"upStr = %@", upStr);

 7         //转换为小写

 8         NSString* lowerStr = [str lowercaseString];

 9         NSLog(@"lowerStr = %@", lowerStr);

10         //转换成首字母大写

11         NSString* newStr = [str capitalizedString];

12         NSLog(@"newStr = %@", newStr);

13         

14         //字符串提取

15         //指定位置到结尾:

16         NSString* subStr = [str substringFromIndex:7];

17         NSLog(@"subStr = %@", subStr);

18         //从指定位置提取指定长度(通过 NSRange)

19         subStr = [str substringWithRange: NSMakeRange(7, 7)];

20          NSLog(@"subStr = %@", subStr);

21         //开头到指定位置

22         subStr = [str substringToIndex:6];

23          NSLog(@"subStr = %@", subStr);

24         

25         //字符串切割(componentsSeparatedByString:)方法

26         //此方法根据传递的字符串进行分割,并将分割的结果以数组返回

27         NSArray* strArr = [str componentsSeparatedByString:@" "];

28         for (id str in strArr) {

29             NSLog(@"%@", str);

30         }

31         //拼接数组中得字符串(按指定字符串)

32         NSString* myStr = [strArr componentsJoinedByString:@"__"];

33         NSLog(@"mystr = %@", myStr);

34         //根据传递进来的NSCharacterSet集合进行分割(示例中包含逗号“,”和空格“ ”)

35         strArr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@", "]];

36         for (id str in strArr) {

37             NSLog(@"%@", str);

38         }

 

你可能感兴趣的:(NSString)