Split up NSString using a comma

2020-01-28 04:45发布

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

5条回答
Juvenile、少年°
2楼-- · 2020-01-28 05:26

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

查看更多
爷的心禁止访问
3楼-- · 2020-01-28 05:27

You want:

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

using @"," as separator.

查看更多
Deceive 欺骗
4楼-- · 2020-01-28 05:31
NSString* myString = @"32.0235, 1.345".
NSArray* myArray = [myString  componentsSeparatedByString:@","];

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

See in documentation

查看更多
Emotional °昔
5楼-- · 2020-01-28 05:33

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

NSString* firstString = [[myString componentsSeparatedByString:@","] objectAtIndex:0];
查看更多
霸刀☆藐视天下
6楼-- · 2020-01-28 05:41
NSArray *strings = [coords componentsSeparatedByString:@","];
查看更多
登录 后发表回答