遍历字符串

在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]);

   }  

   

posted @ 2013-07-29 11:29  alincexiaohao  阅读(450)  评论(0编辑  收藏  举报