Christian Stewart
Christian Stewart

Reputation: 15519

Objective-C Split()?

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

Upvotes: 119

Views: 70394

Answers (5)

mipadi
mipadi

Reputation: 411072

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)

Upvotes: 212

UMUT
UMUT

Reputation: 144

if you want access first word:

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

Upvotes: 1

Josep Escobar
Josep Escobar

Reputation: 456

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

Upvotes: 4

Prabh
Prabh

Reputation: 2474

Try this:

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

Upvotes: 95

amrox
amrox

Reputation: 6247

Try componentsSeparatedByString:

Upvotes: 6

Related Questions