如何在iOS上将字符串分割成子字符串?

时间:2022-09-01 05:56:34

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

我从服务器收到一个nsstring。现在我想把它分割成我需要的子字符串。如何分割绳子?

For example:

例如:

substring1:read from the second character to 5th character

substring1:从第二个字符读到第五个字符

substring2:read 10 characters from the 6th character.

substring2:从第6个字符中读取10个字符。

3 个解决方案

#1


213  

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

您还可以使用NString的componentsseparation bystring方法通过子字符串来分割字符串。

Example from documentation:

文档的例子:

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

#2


38  

NSString has a few methods for this:

NSString对此有一些方法:

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

Check the documentation for NSString for more information.

查看NSString的文档以获得更多信息。

#3


0  

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.

我编写了一个小方法来在指定数量的部分中拆分字符串。注意,它只支持单个分隔符。但我认为这是分割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;
}

#1


213  

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

您还可以使用NString的componentsseparation bystring方法通过子字符串来分割字符串。

Example from documentation:

文档的例子:

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

#2


38  

NSString has a few methods for this:

NSString对此有一些方法:

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

Check the documentation for NSString for more information.

查看NSString的文档以获得更多信息。

#3


0  

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.

我编写了一个小方法来在指定数量的部分中拆分字符串。注意,它只支持单个分隔符。但我认为这是分割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;
}