xcode 如何将字符串转换为带分隔符的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6111543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:06:10  来源:igfitidea点击:

how can I convert string to an array with separator?

iphoneobjective-cxcodensstringnsarray

提问by Christina

I have a string in the following format

我有以下格式的字符串

myString = "cat+dog+cow"

I need to store each string separated by + in to a array. Eg:

我需要将每个由 + 分隔的字符串存储到一个数组中。例如:

myArray[0] = cat
myArray[1] = dog
myArray[2] = cow

Can anyone tell me the proper way to do this?

谁能告诉我正确的方法来做到这一点?

回答by Anish

componentsSeparatedByString:splits the string and return the result in an array.

componentsSeparatedByString:拆分字符串并将结果返回到数组中。

NSArray *myArray = [myString componentsSeparatedByString:@"+"];

[myArray objectAtIndex:0];//cat
[myArray objectAtIndex:1];//dog
[myArray objectAtIndex:2];//cow

回答by Anish

Try this..

尝试这个..

NSArray *arr = [myString componentsSeparatedByString:@"-"];

[arr objectAtIndex:0];//Hai
[arr objectAtIndex:1];//Welcome

回答by Jhaliya

Use the componentsSeparatedByString:method of NSString.

使用NSStringcomponentsSeparatedByString:方法。

 NSString string = @"hai-welcome";
 NSArray myArray = [string componentsSeparatedByString:@"-"];

NSString* haiString = [myArray objectAtIndex:0];
NSString* welcomeString = [myArray objectAtIndex:1];

回答by Midhere

It is vert simple..

它非常简单..

NSString * test = @"Hello-hi-splitting-for-test";
NSArray * stringArray = [test componentsSeparatedByString:@"-"];

// Now stringArray will contain all splitted strings.. :)

Hope this helps...

希望这可以帮助...

I you don't want use array then iterate through each character...

我你不想使用数组然后遍历每个字符......

NSMutableString * splittedString = nil;

for(int i=0;i<test.length;i++){

    unichar character = [test characterAtIndex:0];
    if (character=='-') {

        if (splittedString!=nil) {
            NSLog(@"String component %@",splittedString);
            [splittedString release];
            splittedString = nil;
        }

    } else {
        if (splittedString==nil) {
            splittedString = [[NSMutableString alloc] init];                
        }
        [splittedString appendFormat:@"%C",character];
    }

}

if (splittedString!=nil) {
    NSLog(@"String last component %@",splittedString);
    [splittedString release];
    splittedString = nil;
}

Thats all...

就这样...

回答by memmons

NSArray *myWords = [myString componentsSeparatedByString:@"+"];

回答by Anjani

You can find this one very simple

你会发现这个很简单

NSString *str = @"cat+dog+cow";
NSArray *arr = [str componentsSeparatedByString:@"+"];
NSLog(@"Array items %@",arr);

OUTPUT:

输出:

Array items ( Cat, dog, Cow )

数组项(猫、狗、牛)

回答by Jayprakash Dubey

Simple code :

简单代码:

NSString *myString = [[NSString alloc] initWithString:@"cat+dog+cow"];
NSArray *resultArray = [tempString componentsSeparatedByString:@"+"];

NSLog(@"1. %@ 2. %@ 3. %@",[resultArray objectAtIndex:0],[resultArray objectAtIndex:1],[resultArray objectAtIndex:2]);

回答by Nick

Try This:

尝试这个:

NSString *str = @"cat+dog+cow" ;
NSArray *array = [str componentsSeparatedByString:@"+"];
NSLog(@"%@",array) ;

回答by Chetan Bhalara

NSArray *strArray = [myString componentsSeparatedByString:@"-"];

firstString = [strArray objectAtIndex:0];//Hai
secondString = [strArray objectAtIndex:1];//Welcome

回答by Narayanan Ramamoorthy

NSArray *lines = [string componentsSeparatedByString:@"-"];

The first value will be stored in 0th index of linesand second value will be stored in 1th index of lines..

第一个值将存储在第 0索引中,第二个值将存储在第 1索引中。

Why not array?

为什么不是数组?