Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split up NSString using a comma

I have a JSON feed connected to my app. One of the items is lat & long separated by a comma. For example: "32.0235, 1.345".

I'm trying to split this up into two separate values by splitting at the comma.

Any advice? Thanks!!

like image 645
Adam Storr Avatar asked Jun 22 '11 16:06

Adam Storr


4 Answers

NSArray *strings = [coords componentsSeparatedByString:@","];
like image 191
Alexander Theißen Avatar answered Nov 05 '22 20:11

Alexander Theißen


NSString* myString = @"32.0235, 1.345".
NSArray* myArray = [myString  componentsSeparatedByString:@","];

NSString* firstString = [myArray objectAtIndex:0];
NSString* secondString = [myArray objectAtIndex:1];

See in documentation

like image 27
Jhaliya - Praveen Sharma Avatar answered Nov 05 '22 22:11

Jhaliya - Praveen Sharma


You want:

- (NSArray *)componentsSeparatedByString:(NSString *)separator

using @"," as separator.

like image 5
MarkPowell Avatar answered Nov 05 '22 20:11

MarkPowell


This is work for me as I was not looking to define any Array.

NSString* firstString = [[myString componentsSeparatedByString:@","] objectAtIndex:0];
like image 2
Sid Avatar answered Nov 05 '22 21:11

Sid