如何组合两个NSString?

发布于 2024-12-06 22:11:25 字数 367 浏览 0 评论 0原文

我有以下两个 NSString:

NSString *latitudeString, *longitudeString;

latitudeString = @"50.1338";

longitudeString = @"8.91583";

我想要获取的是一个如下所示的 NSString:50.1338,8.91583。

我如何获得它?谢谢

重要提示:我显示的值只是为了更好地理解,通常 latitudeStringlongitudeString 具有随机值。

I have the following two NSString:

NSString *latitudeString, *longitudeString;

latitudeString = @"50.1338";

and

longitudeString = @"8.91583";

What I want to obtain is a NSString that looks like this: 50.1338,8.91583.

How do I obtain that?Thanks

IMPORTANT: The values I showed are only for the purpose of better understanding, ussually latitudeString and longitudeString have random values.

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

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

发布评论

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

评论(7

情绪少女 2024-12-13 22:11:25

为了得到你想要的,你可以使用

NSString *coordinates = [NSString stringWithFormat:@"%@,%@", latitudeString, longitudeString];

To get what you want you can use

NSString *coordinates = [NSString stringWithFormat:@"%@,%@", latitudeString, longitudeString];
抚你发端 2024-12-13 22:11:25

只需使用 stringWithFormat 方法

[NSString stringWithFormat:@"%@,%@", latitudeString, longitudeString];

Just use stringWithFormat method

[NSString stringWithFormat:@"%@,%@", latitudeString, longitudeString];
生来就爱笑 2024-12-13 22:11:25

迟了一千年:

[latitudeString stringByAppendingFormat:@",%@", longitudeString]

会稍微降低运行时解析成本,并且从历史上看,会给您带来更高的类型安全性(在编译器检查类型字符串之前)。因此,我们中的一些老手已经习惯了它,但仍然在心理上以相当微弱的性能论点来证明舒适的合理性。

[@[latitudeString, longitudeString] componentsJoinedByString:@","]

如果您宁愿在缺少一个字符串时引发异常,也不愿默默地产生一些无意义的东西,这也可能是更好的选择。

A thousand years late:

[latitudeString stringByAppendingFormat:@",%@", longitudeString]

Would slightly reduce runtime parsing costs and, historically, would have given you greater type safety (before the compiler checked type strings). So some of us old timers got used to it and still mentally make the fairly feeble performance argument to justify what's comfortable.

[@[latitudeString, longitudeString] componentsJoinedByString:@","]

May also be preferable if you'd rather raise an exception upon one string being missing than silently produce something nonsensical.

笑梦风尘 2024-12-13 22:11:25

试试这个

NSString *combine = [NSString stringWithFormat:@"%@,%@", latitudeString, longitudeString];

Try this

NSString *combine = [NSString stringWithFormat:@"%@,%@", latitudeString, longitudeString];
往事随风而去 2024-12-13 22:11:25

也许这样更好:

[NSString stringWithFormat:@"%.4f,%.4f",[latitudeString floatValue],[longitudeString floatValue]];

Maybe it was better:

[NSString stringWithFormat:@"%.4f,%.4f",[latitudeString floatValue],[longitudeString floatValue]];
巷子口的你 2024-12-13 22:11:25
String* coord = [NSString stringWithFormat:@"%@,%@", latitudeString, longitudeString];
String* coord = [NSString stringWithFormat:@"%@,%@", latitudeString, longitudeString];
浊酒尽余欢 2024-12-13 22:11:25

[NSString stringWithFormat:@"%@,%@",latitudeString,longitudeString];

[NSString stringWithFormat:@"%@,%@",latitudeString,longitudeString];

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