Objective C NsString: Managing Immutable Strings

objective c nsstringObjective C is the primary language used to develop Apple’s iOS and OS X operating systems. Even Apple’s Cocoa and Cocoa touch framework work on Objective C, so you need to know the language to develop iTunes apps. Objective C is a subset of the C language- it improves on C in many ways, but programs written in C can still be run on Objective C, after a little modification.  Like C, Objective C can be used as a structural programming language. However, Objective C is also an object-oriented programming language. Object-oriented programming languages, like Java and C++, support concepts like inheritance and polymorphism. Objective C is no different. In this tutorial, we’re going to take a look at the NSString Class in Objective C, which inherits from the NSObject class. We’ll also take a look at some of the important methods of the NSString Class. As long as you’re familiar with the basics of OOP and Objective C, you shouldn’t have a problem with this beginner tutorial. But if you’re new to Objective C, you may first want to take this beginners course to help get started.

The NSString Class

The NSString class lets you declare a programmatic interface to manage immutable strings using an object. In non-technical terms, it lets you create and store strings (or text) that cannot be modified in an object. The NSString class also has many methods that let you manipulate the strings that you are storing. Once a string has been instantiated with NSString, it cannot be modified. NSString does have a subclass called the NSMutableString, however, which does let you modify strings.

Creating a String with NSString

The basic syntax of the NSString class is something like this:

NSString *object_name = @”String_message”;

Let’s write a short program that creates a couple of strings and prints them using NSLog():

#import <Foundation/Foundation.h>
int main ()
NSString *messageone = @"This is a string";
NSString *messagetwo = @”created with NSString”;
NSLog(@"Display: %@%@ \n", messageone, messagetwo );
return 0;


Display: This is a string created with NSSTring

Here, we instantiated two strings with the help of two objects, and we printed the two strings with NSLog(). The format to create a string is similar to the format we follow with NSLog(). To learn more about NSLog(), you can take a look at some of the tutorials we’ve written on the topic. Alternatively, you can sign up for this Objective C course to learn how to write your own programs.

Using The NSSTring stringWithFormat Method

The NSString class has several useful methods in it. One of those methods is the stringWithFormat method, as we show in the program below:

NSString *book = @”Lord of the Rings”;
NSString *genre = @”fantasy”;
NString *print = [NSString stringWithFormat: @”The book %@ is the greatest work of %@ literature” book, genre];
NSLog (@”%@”, print);


The book Lord of the Rings is the greatest work of fantasy literature

Here, we created three separate objects: book, genre and print. We used these objects to instantiate three different string values. We used the NSString stringWithFormat method with the third object print.The NSString stringWithFormat method accepts a string value. The format we provide is used as a template for the string and allows string values to be substituted where necessary. Finally, on the last line of the program we printed the string within the print object.

Combining Two Strings with the NSString stringByAppendingString Method

You can concatenate (or combine) two strings into a third separate string with the stringByAppendingString method. However, your original two strings will remain intact- remember, NSString creates immutable string objects. With this method, you will obtain a new separate third string. Take a look at this program:

NSString *world = @"This world";
NSString *shape = @"is round";
NSString *combined = [world stringByAppendingString:shape];
NSLog(@"%@", combined);


This worldis round

Here, we combined the two strings objects world and shape into a third separate string object called combined. Notice the lack of a whitespace character “ “ between the words world and round. The strings get combined without blanks if we don’t specify them. This is a minor shortcoming of the stringByAppendingString method, which can be overcome by the stringByAppendingFormat method, like we show below.

Combining Two Strings with the NSString stringByAppendingFormat

The only difference between the stringByAppendingString method and the stringByAppendingFormat method is that we have to specify a format for the latter. Take a look at the program below:

NSString *world = @"This world";
NSString *shape = @"is round\n";
NSString *combined = [world stringByAppendingString:shape];
NSLog(@"%@", combined);
combined = [world stringByAppendingFormat:@" %@", shape];
NSLog(@"%@", combined);


This worldis round
This world is round

You can see that we had to specify a format after declaring the stringByAppendingFormat method. In the format, all we did was use a whitespace character before declaring it with shape – and as a result, our output changed.

Creating Mutable Strings with NSMutableString

NSString, like we mentioned earlier, doesn’t let you modify a string once you create it. You can create modifiable strings with the NSMutableString subset class. You also have to use the stringWithString method to turn your string mutable. Take a look at this example program below:

NSMutableString *book = [NSMutableString stringWithString:@"Lord of the Rings"];
NSLog(@”Old String: %@\n”, book);
[book setString:@"Treasure Island"];
NSLog(@”New String: %@\n”, book);


Lord of the Rings
Treasure Island

The program is pretty much self explanatory. The object book, which held a different string at first, now holds a different string.

There are several other useful methods in the NSString class that you should know. To learn more about them, you can sign up for this Objective C course.You will encounter the NSSTring class a lot if you take a look at working apps. Once you’re ready to move on to the next level, you can take this course to learn how to make your own iPhone or iPad app with Objective C.