47

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!!

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Adam Storr
  • 1,438
  • 2
  • 21
  • 46

5 Answers5

133
NSArray *strings = [coords componentsSeparatedByString:@","];
Alexander Theißen
  • 3,799
  • 4
  • 28
  • 35
17
NSString* myString = @"32.0235, 1.345".
NSArray* myArray = [myString  componentsSeparatedByString:@","];

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

See in documentation

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
5

You want:

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

using @"," as separator.

MarkPowell
  • 16,482
  • 7
  • 61
  • 77
2

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

NSString* firstString = [[myString componentsSeparatedByString:@","] objectAtIndex:0];
Sid
  • 137
  • 1
  • 1
1

Try [yourCommaSeparatedString componentsSeparatedByString:@", "]
that will give a NSArray with strings you can then call floatValue on ;)

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217