OC-遍历字符串

来源:互联网 发布:2012-2015淘宝交易数据 编辑:程序博客网 时间:2024/05/23 13:52

遍历字符串

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

   }  

0 0
原创粉丝点击