how can I convert string to an array with separator?

后端 未结 12 1937
故里飘歌
故里飘歌 2020-12-09 07:26

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:



        
相关标签:
12条回答
  • 2020-12-09 07:32

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

    0 讨论(0)
  • 2020-12-09 07:33
    NSArray *myWords = [myString componentsSeparatedByString:@"+"];
    
    0 讨论(0)
  • 2020-12-09 07:34

    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]);
    
    0 讨论(0)
  • 2020-12-09 07:39

    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
    
    0 讨论(0)
  • 2020-12-09 07:39

    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];
    
    0 讨论(0)
  • 2020-12-09 07:47

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

    0 讨论(0)
提交回复
热议问题