how can I convert string to an array with separator?

拜拜、爱过 提交于 2019-11-28 07:53:07

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

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

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

Try this..

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

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

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...

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

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 )

Jhaliya

Use the componentsSeparatedByString: method of NSString.

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

NSString* haiString = [myArray objectAtIndex:0];
NSString* welcomeString = [myArray objectAtIndex:1];
NSArray *strArray = [myString componentsSeparatedByString:@"-"];

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

This will be the solution if you are dealing with a string:

NSString *mySstring = @"hai-welcome";
NSMutableArray *anArray=[[NSMutableArray alloc] initWithArray:[componentsSeparatedByString: @"-"]];

And each word will be stored in respective position from 0-n.

Try This. :)

If you are averse to using arrays, you can consider this –

NSString *accessMode, *message;

NSScanner *scanner = [NSScanner scannerWithString:@"hai-welcome"];
NSCharacterSet *hyphenSet = [NSCharacterSet characterSetWithCharactersInString:@"-"];

[scanner scanUpToCharactersFromSet:hyphenSet intoString:&accessMode];
[scanner scanCharactersFromSet:hyphenSet intoString:nil];
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@""] intoString:&message];
Narayanan Ramamoorthy
NSArray *lines = [string componentsSeparatedByString:@"-"];

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

Why not array?

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]);

Try This:

NSString *str = @"cat+dog+cow" ;
NSArray *array = [str componentsSeparatedByString:@"+"];
NSLog(@"%@",array) ;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!