简体   繁体   English

如何结合两个NSString?

[英]How to combine two NSString?

I have the following two NSString : 我有以下两个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. 我想要获得的是一个NSString,如下所示: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. 重要提示:我显示的值仅用于更好地理解,因此latitudeStringlongitudeString具有随机值。

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

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

只需使用stringWithFormat方法

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

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. 如果你想在一个字符串丢失时提出异常而不是默默地产生一些荒谬的东西,也可能更好。

试试这个

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

也许它更好:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM