如何将NSString子字符串化为字符的第三次出现? [英] How could I substring an NSString to the third occurrence of a character?

查看:61
本文介绍了如何将NSString子字符串化为字符的第三次出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重复3次后,我想计算','字符和子字符串.

I want to count the ',' characters and substring when it has been repeated 3 times.

输入:

9,1,2,3,9

输出:

9,1,2

推荐答案

我很确定某些特定的东西没有内置方法. 在我的头顶上:

I am pretty sure something that specific would not have a build in method. Off the top of my head:

for(unsigned int i = 0; i < [string length]; ++i)
{
    if([string characterAtIndex:i] == ',')
    {
         ++commaCount;
         if(commaCount == 3)
         {
              return [string substringWithRange: NSMakeRange(0, i)];
         }
     }
}

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

苹果参考文件通常是一个很好的起点.

The apple reference documents are usually a good place to start.

这篇关于如何将NSString子字符串化为字符的第三次出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆