Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I substring an NSString to the third occurrence of a character?

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
like image 571
RAGOpoR Avatar asked Jan 18 '26 14:01

RAGOpoR


1 Answers

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.

like image 199
eyesathousand Avatar answered Jan 21 '26 07:01

eyesathousand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!