Objective-c 字符串遍历的几种方法?

原文地址  http://blog.csdn.net/xinshou_jiaoming/article/details/7068199

遍历字符串

在oc中遍历字符串的至少可以使用以下两种方法

(1) 通过查找的方式来(这方式适合所有格式的子符串,推荐使用)

NSString *newStr =@"abdcdddccdd00大家好哦";

NSString *temp = nil;

for(int i =0; i < [newStr length]; i++)

{

temp = [newStr substringWithRange:NSMakeRange(i, 1)];

NSLog(@"第%d个字是:%@",i,temp);

}

(2) 通过遍历字符的方式遍历字符串(只适合不包含中文的字符串)

NSString *newStr = @"abdcdddccdd00";

for(int i =0; i < [newStr length]; i++)

{

NSLog(@"第%d个字符是:%@",i, [newStr characterAtIndex:i]);

}