ISO C++ String Class

ISO C++ has a string class in the Standard Library. This handout is not a complete listing of the facilities of the string class, but it describes the basic features. Older C++ compilers may not include this class. The Turbo C++ compiler we used last semester does not have it.

To use the ISO C++ string class, you must include the header file <string>. The string class takes care of allocating the storage to hold the string and it also keeps track of the string length. When you use a string object you never need to worry about the null byte, as you did with C-style strings.

Declaration: String objects are declared as type string, such as:

      string name;

Creating and Initializing strings: By default, a string object is initialized to the empty string (a string that contains no characters). A string object can be initialized with a string literal (a string literal is a sequence of characters inside double quotes), or with another string object, as in the following examples:

     string name("Jane Austen");
     string another;
     string copy(name);

The statements above will create three string objects, name, another, and copy. The string objects name, and copy both contain the characters Jane Austen, and the string object another contains the empty string.

Assignment: The assignment operator is defined for string objects to assign values from other string objects or from string literals, as in the following examples:

     string name;
     name = "Jane Austen";
     string another;
     another = name;

Input: There are two ways to read into a string object. The extraction operator >> skips over leading whitespace, then reads all characters until the next whitespace character is seen. The trailing whitespace character is not read; it is left in the input stream. To read an entire line of input, including whitespace, use getline. Getline reads all characters up to a newline ('\n') character. The newline is read but not put into the string object. For example, suppose the input contains:

BBpersuasionBBBBsenseBandBsensibility

where each 'B' represents a blank, and the following statements are executed:

     string name;
     cin >> name;
     string another;
     getline(cin,another);

Then the string objects will have the following values:

     name = "persuasion"
     another = "BBBBsenseBandBsensibility"

Output: The insertion operator << is used to print a string object. It will print all characters contained in the string. If the string is empty, nothing will be printed.

Relational Operators: The relational operators ==, !=, <, <=, >, and >= are available for comparing string objects and string literals. At least one of the operands must be a string object. The comparisons are done using the ASCII collating sequence, so A < B < ... < Z < 0 < 1 < ... < 9 < a < b < ... < z. For example, the following are valid if statements:

     string name("persuasion");
     string another(name);
     if (name == another)
         ...
     if (another <= "pride and prejudice")
         ...

Concatenation: The + operator is used to concatenate string objects and string literals. At least one of the operands must be a string object. The + operator creates a new string object formed by the characters in the first operand followed by the characters in the second operand. The operands are not changed by the + operator. For example, the following statements:

     string first,full;
     first = "snow";
     string full = first + "falling";

Then the string objects will have the following values:

     first = "snow"
     full = "snowfalling"

Some Additional Member Functions: There are many member functions in the string class. A couple of those functions are:

The length member function returns the length of the invoking object. For example:
     string saying("all's well that ends well");
     cout << "The length of saying is " << saying.length() << endl;
The output is
     The length of saying is 25
The at member function returns the character in the ith position of the invoking object. The first character in the string is in position 0, so the last character in the string is in position length() - 1. For example:
     string saying("all's well that ends well");
     cout << "The seventh character is " << saying.at(6) << endl;
The output is
     The seventh character is w

Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2001