xcode NSString 解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8712890/
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
NSString parsing
提问by Magnus
I need to parse this string into three different components:
我需要将此字符串解析为三个不同的组件:
Location: 1|#69.83623|#24.432223|#Cupertino, California
The value is stored in one NSString. I need it in three different strings. One string for latitude, one for longitude and one for location.
该值存储在一个 NSString 中。我需要三个不同的字符串。一串表示纬度,一串表示经度,一串表示位置。
Any idea how I can do that?
知道我该怎么做吗?
Thanks!
谢谢!
回答by Thomas Clayson
You can use this method to get an array of different components:
您可以使用此方法获取不同组件的数组:
NSArray *bits = [locationString componentsSeparatedByString: @"|#"];
Each item in the NSArray will be an NSString.
NSArray 中的每一项都是一个 NSString。
回答by Ilanchezhian
Try the following
尝试以下
NSString *location = @"1|#69.83623|#24.432223|#Cupertino, California";
NSArray *components = [location componentsSeparatedByString:@"|#"];
NSLog(@"%@",components);
float latitude = [[components objectAtIndex:1] floatValue];
float longitude = [[components objectAtIndex:2] floatValue];
NSString *loc = [components objectAtIndex:3];
回答by CarlJ
NSString *t = @"Location: 1|#69.83623|#24.432223|#Cupertino, California";
NSArray *k = [t componentsSeparatedByString:@"|"];
NSLog(@"components %@", k);