如何将 NSString 子串到第三次出现的字符?

发布于 2024-12-05 16:05:06 字数 124 浏览 3 评论 0原文

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

输入:

9,1,2,3,9

输出:

9,1,2

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

Input:

9,1,2,3,9

Output:

9,1,2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み格子的夏天 2024-12-12 16:05:06

我很确定特定的东西不会有内置方法。
从我的头顶上:

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

苹果参考文档通常是一个很好的起点。

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.

落在眉间の轻吻 2024-12-12 16:05:06
[yourString substringFromIndex:[yourString rangeOfString:@","].location]

类似的事情。

[yourString substringFromIndex:[yourString rangeOfString:@","].location]

Something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文