How to concatenate strings (i.e. print several at a time)

This is really simple in PHP, however it’s not obvious in Objective-C because you’re not printing the strings directly. Rather you print two objects that point to each string. There is a method though which has the same effect: stringByAppendingString. Here’s how you use it:

It gets a bit more complex when you want to add three or more together; each stringByAppendingString expression needs to be in [brackets] before you add another one, like so:

2 thoughts on “How to concatenate strings (i.e. print several at a time)

  1. If you just want to print several strings that don’t necessarily have to become a single new string you can also choose to print them like this:

    The %@ will be replaced with nameString. You can add as many as you like.

  2. Alternatively, we can format a string with another string, like this:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.