adrian
adrian

Reputation: 4594

How to combine two NSString?

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.

Upvotes: 20

Views: 32244

Answers (7)

Tommy
Tommy

Reputation: 100652

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.

Upvotes: 5

Fran Sevillano
Fran Sevillano

Reputation: 8163

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

Upvotes: 1

hypercrypt
hypercrypt

Reputation: 15376

To get what you want you can use

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

Upvotes: 61

yellowsea
yellowsea

Reputation: 51

Maybe it was better:

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

Upvotes: 4

Rahul Choudhary
Rahul Choudhary

Reputation: 3809

Try this

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

Upvotes: 4

Robert
Robert

Reputation: 38223

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

Upvotes: 2

EXC_BAD_ACCESS
EXC_BAD_ACCESS

Reputation: 2707

Just use stringWithFormat method

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

Upvotes: 8

Related Questions