Split up NSString using a comma

一笑奈何 提交于 2019-11-26 13:47:53

问题


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


回答1:


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



回答2:


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

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

See in documentation




回答3:


You want:

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

using @"," as separator.




回答4:


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

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



回答5:


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



来源:https://stackoverflow.com/questions/6443535/split-up-nsstring-using-a-comma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!