Objective-C Split()?

IphoneObjective CFunctionSplit

Iphone Problem Overview


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)?

Iphone Solutions


Solution 1 - Iphone

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)

Solution 2 - Iphone

Try this:

    NSString *testString= @"It's a rainy day";
    NSArray *array = [testString componentsSeparatedByString:@" "];

Solution 3 - Iphone

Solution 4 - Iphone

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

Solution 5 - Iphone

if you want access first word:

[[string componentsSeparatedByString:@" "] objectAtIndex:0];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristian StewartView Question on Stackoverflow
Solution 1 - IphonemipadiView Answer on Stackoverflow
Solution 2 - IphonePrabhView Answer on Stackoverflow
Solution 3 - IphoneamroxView Answer on Stackoverflow
Solution 4 - IphoneJosep EscobarView Answer on Stackoverflow
Solution 5 - IphoneUMUTView Answer on Stackoverflow