ios 如何组合两个 NSString?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7580299/
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-08-30 21:40:13  来源:igfitidea点击:

How to combine two NSString?

iphoneiosobjective-cxcodensstring

提问by adrian

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 latitudeStringand longitudeStringhave random values.

重要提示:我发现该值仅是为了更好地理解的目的,ussuallylatitudeStringlongitudeString具有随机值。

回答by hypercrypt

To get what you want you can use

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

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

回答by EXC_BAD_ACCESS

Just use stringWithFormat method

只需使用 stringWithFormat 方法

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

回答by Tommy

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.

如果您宁愿在一个字符串丢失时引发异常而不是默默地产生一些无意义的东西,也可能更可取。

回答by yellowsea

Maybe it was better:

也许更好:

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

回答by Rahul Choudhary

Try this

尝试这个

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

回答by Robert

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

回答by Fran Sevillano

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

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