开发者

Split up NSString using a comma

开发者 https://www.devze.com 2023-03-15 04:16 出处:网络
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 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 u开发者_高级运维p into two separate values by splitting at the comma.

Any advice? Thanks!!


NSArray *strings = [coords componentsSeparatedByString:@","];


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

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

See in documentation


You want:

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

using @"," as separator.


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

NSString* firstString = [[myString componentsSeparatedByString:@","] objectAtIndex:0];


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

0

精彩评论

暂无评论...
验证码 换一张
取 消