how can I convert string to an array with separator?

后端 未结 12 1938
故里飘歌
故里飘歌 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:48

    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 )

    0 讨论(0)
  • 2020-12-09 07:48

    Try This:

    NSString *str = @"cat+dog+cow" ;
    NSArray *array = [str componentsSeparatedByString:@"+"];
    NSLog(@"%@",array) ;
    
    0 讨论(0)
  • 2020-12-09 07:54
    NSArray *strArray = [myString componentsSeparatedByString:@"-"];
    
    firstString = [strArray objectAtIndex:0];//Hai
    secondString = [strArray objectAtIndex:1];//Welcome
    
    0 讨论(0)
  • 2020-12-09 07:55

    Try this..

    NSArray *arr = [myString componentsSeparatedByString:@"-"];
    
    [arr objectAtIndex:0];//Hai
    [arr objectAtIndex:1];//Welcome
    
    0 讨论(0)
  • 2020-12-09 07:55
    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?

    0 讨论(0)
  • 2020-12-09 07:56

    Use the componentsSeparatedByString: method of NSString.

     NSString string = @"hai-welcome";
     NSArray myArray = [string componentsSeparatedByString:@"-"];
    
    NSString* haiString = [myArray objectAtIndex:0];
    NSString* welcomeString = [myArray objectAtIndex:1];
    
    0 讨论(0)
提交回复
热议问题