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

   }  

  

posted @ 2012-08-26 15:24  疯狂的萝卜  阅读(1247)  评论(0编辑  收藏  举报