Changes

Jump to: navigation, search

Working with String Objects in Objective-C

3,990 bytes added, 19:39, 2 November 2009
New page: Strings are collections of characters that are grouped together to form words or sentences. If it wasn't for humans, computers would probably never have anything to do with strings. The fa...
Strings are collections of characters that are grouped together to form words or sentences. If it wasn't for humans, computers would probably never have anything to do with strings. The fact is, however, that one of the primary jobs of a computer is to accept data from and present data to humans. For this reason it is highly likely that any Objective-C program is going to involve a considerable amount of code specifically designed to work with data in the form of strings. The purpose of this chapter is to cover the key aspects of string creation, comparison and manipulation in Objective-C using the Foundation Framework's NSString class.

== Strings without NSString ==

As we've already discussed, Objective-C is built upon the C programming language. C also has a mechanism for dealing with strings, but because C is not an object oriented programming language it does not have any of the advantages offered to us through the NSString class. For example, to use the C approach to creating strings we have to set up a pointer to a string of characters:

<pre>
char *myString = "This is a C character string";
</pre>

Alternatively, we could define a C style character array to contain a string:

<pre>
char myString[] = "This is a C character array";
</pre>

Having created the string we literally have nothing but a section of memory where a null terminated string of characters is located. If we want to do anything to manipulate the string we will have to write our own code to do it.

By using NSString, however, we get access to a wide range of methods provided with that class to manipulate our string without having to write all the code ourselves. My first ever job involved writing complex programs in the C programming language, and from bitter experience I can tell you that the hardest part of that job was writing code to work with strings. My life would have been much easier if Objective-C and the NSString class had existed all those years ago.

In addition, C style character strings are composed of single byte characters and therefore limited in the range of characters that can be stored. This usually becomes a problem when dealing with non-ASCII character sets used by foreign languages. NSString objects, on the other hand, are comprised of multibyte Unicode characters and can handle just about any language character set.


== Declaring Constant String Objects ==

A constant string object is declared by encapsulating the string in double quotes (") and preceded by an @ sign. For example:

<pre>
@"This is a constant character string object";
</pre>

In order to display the current value of a string object using the NSLog methods, simply reference the string using '%@' as follows:

<pre>
NSLog (@"%@", @"This is a constant character string object");
</pre>

Even though all we are doing here is creating a constant string object, keep in mind that this is still an object. As such, it has a range of methods that we can call on it. For examples string objects have a ''length'' methods that returns the number of characters in the string. We can, therefore, call this on a constant string object:

<pre>
int len = [@"Hello" length];

NSLog (@"Length of string = %i", len);
</pre>

The above code declares a constant string object containing the word "Hello" and calls the ''length'' method of object. The result is assigned to an integer variable named ''len'' which in turn is displayed using NSLog. When compiled and executed, we get the following output:

<pre>
Length of string = 5
</pre>

== Creating Mutable and Immutable String Objects ==

== Getting the Length of a String ==

== Copying a String ==

== Searching for a Substring ==

== Replacing Parts of a String ==

== Deleting Sections of a String ==

== Extracting a Subsection of a String ==

== Appending Text to the End of a String ==

== Comparing Strings ==

== Checking for String Prefixes and Suffixes ==

== Converting to Upper or Lower Case ==

== Converting Strings to Numbers ==

Navigation menu