如何从字符串中提取值

发布于 2024-12-17 06:48:32 字数 159 浏览 1 评论 0原文

我有一个字符串... "<+36.83452988, -119.79611969> +/- 0.00m (速度 -1.00 mps / 路线 -1.00) @ 11/21/11 8:12:18 PM 印度标准时间“

我希望从中提取坐标。怎么办呢..?

i have a string ... "<+36.83452988, -119.79611969> +/- 0.00m (speed -1.00 mps / course -1.00) @ 11/21/11 8:12:18 PM India Standard Time"

I wish to extract the co-ordinates out of it. how can it be done..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情定在深秋 2024-12-24 06:48:32

乔纳斯的简单基本思想是正确的,只是不是用你的语言表达的。
它很简单,最终会为您提供 x 和 y 坐标。

您可以使用 NSString rangeOfString:substringWithRange: 方法自行实现 Jonas 建议,并担心此行后面的空格

[string stringByReplacingOccurrencesOfString:@" " withString:@""];

以下代码将采用您的示例字符串并打破它为您转换为字符串和浮点值,无论您喜欢哪种。

NSString * string = @"<+36.83452988, -119.79611969> +/- 0.00m (speed -1.00 mps / course -1.00) @ 11/21/11 8:12:18 PM India Standard Time>";
NSRange startX = [string rangeOfString:@"<"];
NSRange endX = [string rangeOfString:@","];
NSRange endY = [string rangeOfString:@">"];

NSRange xRange;
xRange.location = startX.location+1;
xRange.length = endX.location - startX.location -1;

NSRange yRange;
yRange.location = (xRange.length + xRange.location) + 1;
yRange.length = endY.location - yRange.location;

float x = [[string substringWithRange:xRange] floatValue];
float y = [[string substringWithRange:yRange] floatValue];

NSLog(@"xString-- %@\nyString-- %@", [string substringWithRange:xRange], [string substringWithRange:yRange]);
NSLog(@"%f, %f", x,y);

Jonas has the simple fundamental idea right, just not in your language.
It is simple and ends up with an x and y coordinate for you.

You can implement Jonas suggestion yourself with the NSString rangeOfString: and substringWithRange: methods and worry about the white space after with this line

[string stringByReplacingOccurrencesOfString:@" " withString:@""];

The following code will take your example string and break it into string and float values for you, whichever you prefer.

NSString * string = @"<+36.83452988, -119.79611969> +/- 0.00m (speed -1.00 mps / course -1.00) @ 11/21/11 8:12:18 PM India Standard Time>";
NSRange startX = [string rangeOfString:@"<"];
NSRange endX = [string rangeOfString:@","];
NSRange endY = [string rangeOfString:@">"];

NSRange xRange;
xRange.location = startX.location+1;
xRange.length = endX.location - startX.location -1;

NSRange yRange;
yRange.location = (xRange.length + xRange.location) + 1;
yRange.length = endY.location - yRange.location;

float x = [[string substringWithRange:xRange] floatValue];
float y = [[string substringWithRange:yRange] floatValue];

NSLog(@"xString-- %@\nyString-- %@", [string substringWithRange:xRange], [string substringWithRange:yRange]);
NSLog(@"%f, %f", x,y);
神也荒唐 2024-12-24 06:48:32

如果坐标是字符串的第一部分,您应该只获取基于某些字符的子字符串,如下所示:

String x = s.substring(s.indexOf('<')+1, s.indexOf(',')).trim();
String y = s.substring(s.indexOf(',')+1, s.indexOf('>')).trim();

这是获取 << 之间的部分。和 ,然后在 、 和 > 之间。

问候

乔纳斯

If the coordinates are the first part of your String you should just get substrings bases on certain characters, like that:

String x = s.substring(s.indexOf('<')+1, s.indexOf(',')).trim();
String y = s.substring(s.indexOf(',')+1, s.indexOf('>')).trim();

This is getting the part between < and , and then between , and >.

Regards

Jonas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文