How to combine two NSString?

31,161

Solution 1

To get what you want you can use

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

Solution 2

Just use stringWithFormat method

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

Solution 3

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.

Solution 4

Try this

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

Solution 5

Maybe it was better:

[NSString stringWithFormat:@"%.4f,%.4f",[latitudeString floatValue],[longitudeString floatValue]];
Share:
31,161
adrian
Author by

adrian

I've been doing a little bit of everything: android, iphone and now learning titanium!

Updated on July 09, 2022

Comments

  • adrian
    adrian almost 2 years

    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.