json 使用逗号拆分 NSString

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6443535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:54:06  来源:igfitidea点击:

Split up NSString using a comma

iphonejsonnsstringcomma

提问by Adam Storr

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".

我有一个连接到我的应用程序的 JSON 提要。其中一项是经纬度,以逗号分隔。例如:“32.0235、1.345”。

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

我试图通过在逗号处拆分来将其拆分为两个单独的值。

Any advice? Thanks!!

有什么建议吗?谢谢!!

回答by Alexander Thei?en

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

回答by Jhaliya

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

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

See in documentation

文档

回答by MarkPowell

You want:

你要:

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

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

using @"," as separator.

使用@"," 作为分隔符。

回答by Sid

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

这对我有用,因为我不想定义任何数组。

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

回答by Martin Ullrich

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

尝试[yourCommaSeparatedString componentsSeparatedByString:@", "]
将给出一个带有字符串的 NSArray,然后您可以在 ;) 上调用 floatValue