How to split string into substrings on iOS?

101,604

Solution 1

You can also split a string by a substring, using NString's componentsSeparatedByString method.

Example from documentation:

NSString *list = @"Norman, Stanley, Fletcher";
NSArray *listItems = [list componentsSeparatedByString:@", "];

Solution 2

NSString has a few methods for this:

[myString substringToIndex:index];
[myString substringFromIndex:index];
[myString substringWithRange:range];

Check the documentation for NSString for more information.

Solution 3

I wrote a little method to split strings in a specified amount of parts. Note that it only supports single separator characters. But I think it is an efficient way to split a NSString.

//split string into given number of parts
-(NSArray*)splitString:(NSString*)string withDelimiter:(NSString*)delimiter inParts:(int)parts{
    NSMutableArray* array = [NSMutableArray array];

    NSUInteger len = [string length];
    unichar buffer[len+1];

    //put separator in buffer
    unichar separator[1];
    [delimiter getCharacters:separator range:NSMakeRange(0, 1)];

    [string getCharacters:buffer range:NSMakeRange(0, len)];

    int startPosition = 0;
    int length = 0;
    for(int i = 0; i < len; i++) {

        //if array is parts-1 and the character was found add it to array
        if (buffer[i]==separator[0] && array.count < parts-1) {
            if (length>0) {
                [array addObject:[string substringWithRange:NSMakeRange(startPosition, length)]];

            }

            startPosition += length+1;
            length = 0;

            if (array.count >= parts-1) {
                break;
            }

        }else{
            length++;
        }

    }

    //add the last part of the string to the array
    [array addObject:[string substringFromIndex:startPosition]];

    return array;
}
Share:
101,604

Related videos on Youtube

Chilly Zhong
Author by

Chilly Zhong

I'm a mobile app developer. I was developing for Windows Mobile and Blackberry. I begin to dig into iPhone Development since Feb, 2009. Working has become a brand-new page for me and for my life. I was working on a symbian app for a short time in March, 2010. And I begin to learn Android in Nov, 2010 and learn WindowsPhone in Nov, 2011. Now I'm working on an iOS framework similar to iAd and I'm happy to offer all kinds of libs to help other developers. Always walking down your own way and keep your faith.

Updated on July 19, 2020

Comments

  • Chilly Zhong
    Chilly Zhong almost 4 years

    I received an NSString from the server. Now I want to split it into the substring which I need. How to split the string?

    For example:

    substring1:read from the second character to 5th character

    substring2:read 10 characters from the 6th character.

    • Ricardo
      Ricardo about 9 years
      You should change iPhone to iOS.
  • Chilly Zhong
    Chilly Zhong about 15 years
    Can I split strings whose separate marks are different? e.g. @"A,B^C~D"
  • codelogic
    codelogic about 15 years
    You should be able to use NSString's "componentsSeparatedByCharactersInSet:" to split on multiple characters.
  • Nikunj Jadav
    Nikunj Jadav over 12 years
    hello I successfully separate string but i want to set that separated in UILabel any idea?
  • Justin
    Justin over 12 years
    Iterate through your array: for (int i = 0; i < [listItems count]; i++) and use [listItems objectAtIndex:i];
  • innuendoreplay
    innuendoreplay almost 12 years
    Thanks! i make a little different: ` NSArray * listItems = [list componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];`