Udemy logo

nsstring appendString concatenation is one of the most commonly-used techniques in modern programming. In most cases, this is accomplished using an addition or concatenation operator. However, you do not do it this way in Objective-C. Strings in Objective-C are handled using the classes NSString and NSMutableString, and require a little more effort to work with on the part of the programmer.

The information presented here is a short description of how to append and concatenate strings in Objective-C; it is not a complete tutorial for the language. Programming in Objective-C requires careful memory management and good practices (particularly if you will be submitting your projects to the iOS or Mac App Stores), which are best learned through a comprehensive tutorial such as Objective-C for Beginners, which teaches application development for OS X and iOS. If you are working exclusively on iOS apps, you can learn the basics of iOS 7 App Development at Udemy.com.

Working with NSString

Instances of the class NSString are immutable – their contents cannot be changed. Once a string has been initialized using NSString, the only way to append text to the string is to create a new NSString object. While doing so, you can append string constants, NSString objects, and other values.

The NSString class contains several methods that you can use for this purpose.

1. stringByAppendingString:(NSString *)

This method returns a new string that is made up of the text from the receiver object, with the text from the argument appended to the end of it.

The string object passed into stringByAppendingString can be a string constant written into the code (as shown below), or another NSString object.

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSString * test = [[NSString alloc] initWithString:@"This is a test string."];
    NSString * test2 = [test stringByAppendingString:@" Adding another string."];
    [test release];
    [pool release];
    return 0;

Multiple strings can be joined by nesting calls to stringByAppendingString:

NSString * string3 = [string1 stringByAppendingString:[string2 stringByAppendingString:@" Adding a third string."]];

2. stringByAppendingFormat:(NSString *) format,…

The method stringByAppendingFormat works in a similar fashion to stringByAppendingString. The difference is that it accepts multiple arguments.

The first argument specifies a string formatter. A string formatter is an instance of NSString which includes insertion points that are replaced with actual values when the formatter is evaluated. Multiple insertion points can be included in the formatter, allowing for an easy way to combine multiple strings.

After the first argument, any subsequent objects that are passed into the method are the values to be inserted into the string. By using the correct format specifiers to create insertion points, the string can be appended with objects of a variety of types. The format specifier “%@” is used to insert string values.

The example below uses a combination of whitespace and %@ format specifiers to append two strings to string1:

NSString * string3 = [string1 stringByAppendingFormat:@" %@ %@", string2, @"A third string."];

The objects are inserted in the order that they are passed into the method.

3. stringWithFormat:(NSString *) format,…

As shown above, you can use string formatters to append multiple strings and values to a receiving NSString object.

You can use the method stringWithFormat to declare and combine strings using a string formatter, but without first allocating a receiving NSString object. Instead, you call stringWithFormat directly from the NSString class.

NSString * string3 = [NSString stringWithFormat:@"%@ %@ %@", string1, string2, @"A third string."];

Releasing the Original Strings

If you refer back to the first example given when discussing stringByAppendingString, you will notice that the first string – test – is released once the new string is created. But test2 is not released.

You must remember to release strings are created using alloc and init methods. Releasing the strings created by stringByAppendingString, stringByAppendingFormat, and stringWithFormat is not necessary and, in fact, will cause an error.

This can cause a particular problem that you should be aware of:

NSString * string1 = [[NSString alloc] initWithString:@"This is a test string."];
string1 = [NSString stringByAppendingString:@" A second string."];

The example above performs an operation similar to string1 = string1 + “ A second string.”, which might be used in many other programming languages. However, in Objective-C, now that the new string has been assigned to string1, there is no way to release the memory used by the original object. To an extent these situations are alleviated by using automatic reference counting, but it is still something that programmers should be aware of.

One way of avoiding many of these problems is not to use alloc and init when declaring strings. However, you may not always have that option when using library routines.

Working with NSMutableString

Sometimes, particularly if you’re going to be appending a lot of strings, it is just clearer to use an NSMutableString object instead. NSMutableString inherits from NSString, but its contents can be modified.

For passing as arguments into other methods, you can simply cast an NSMutableString to NSString.

However, you cannot cast an NSString to NSMutableString to modify its contents. Instead, you can call the method mutableCopy to return an NSMutableString with the text string copied to the new object.

NSString * string1 = [[NSString alloc] initWithString:@"This is a test string."];
NSMutableString * string2 = [string1 mutableCopy];

mutableCopy returns an entirely new object and making changes to this string will not affect the original NSString object. You must remember to release both objects once you are finished working with them.

NSMutableString contains several methods that can be used to change the string, or append and concatenate other strings to it. Unlike the NSString methods discussed so far, the methods of NSMutableString operate directly on the text content of the receiving object, and do not return another object.

Method Description
appendFormat Appends a string using a string formatter object, in a similar fashion to stringByAppendingFormat as discussed earlier.
appendString Adds a string to the end of the receiving NSMutableString.
deleteCharactersInRange Removes the characters defined by an NSRange object.
insertString Inserts a string into the receiving NSMutableString at the specified position.
replaceCharactersInRange Replaces the characters defined by an NSRange object with those in the specified string.
replaceOccurrencesOfString Replaces instances of the specified text, with the contents of a string.
setString Sets the text content of an NSMutableString, replacing all existing data.

Further Study

For more examples and information about appending and concatenating strings in Objective-C, and related memory management techniques, you can refer to Objective C for Beginners at Udemy.com.

Page Last Updated: June 2014

Featured course

iOS 12 & Objective-C - Complete Developer Course

Last Updated December 2018

  • 32 total hours
  • 132 lectures
  • All Levels
4.8 (278)

A Complete iOS 12 and Xcode 10 Course with Objective-C | By Aaron Caines

Explore Course

Objective-C students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo