Documentation Archive Developer
Search
Table of Contents Previous Section

Working With Strings

NSString and NSMutableString objects represent static and dynamic character strings, respectively. They may be searched for substrings, compared with one another, combined into new strings, and so on.

The difference between NSStrings and NSMutableStrings is that you can't change an NSString's contents from its initial character string. While NSMutableString provides methods such as appendString: and setString: to add to or replace the string's contents, there are no such methods available for NSStrings. It's best to use NSStrings wherever possible. Only use an NSMutableString if you need to modify its contents after you create it.

You can create NSStrings with WebScript's at sign (@) syntax for defining constant objects. For example, the following statement creates an NSString object:

	id msg = @"This option is no longer available. Please choose another.";
You can also create string objects with creation methods-methods whose names are preceded by a + and that return new objects. The strings created with the @ syntax are always NSStrings, so they can't be modified. If you use a creation method instead, you can choose to create either an NSString or a NSMutableString. The following code excerpt illustrates the creation of both NSString and NSMutableString objects:

	// Create an immutable string
	id message = [NSString stringWithString:@"Hi"];

	// Create a mutable string
	id message = [NSMutableString stringWithString:@"Hi"];
The methods provided by NSString and NSMutableString are described in more detail in the next section.

Table of Contents Next Section