Objective-C Split()?

前端 未结 5 1550
小蘑菇
小蘑菇 2020-12-04 15:00

Is there any way to split strings in objective c into arrays? I mean like this - input string Yes:0:42:value into an array of (Yes,0,42,value)?

相关标签:
5条回答
  • 2020-12-04 15:23
    NSArray *arrayOfComponents = [yourString componentsSeparatedByString:@":"];
    

    where yourString contains @"one:two:three"

    and arrayOfComponents will contain @[@"one", @"two", @"three"]

    and you can access each with NSString *comp1 = arrayOfComponents[0];

    (https://developer.apple.com/documentation/foundation/nsstring/1413214-componentsseparatedbystring)

    0 讨论(0)
  • 2020-12-04 15:29

    if you want access first word:

    [[string componentsSeparatedByString:@" "] objectAtIndex:0];
    
    0 讨论(0)
  • 2020-12-04 15:31

    Use this: [[string componentsSeparatedByString:@","][0];

    0 讨论(0)
  • 2020-12-04 15:40

    Try this:

        NSString *testString= @"It's a rainy day";
        NSArray *array = [testString componentsSeparatedByString:@" "];
    
    0 讨论(0)
  • 2020-12-04 15:46

    Try componentsSeparatedByString:

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