Chapter 4: The `string' data type

We're always interested in getting feedback. E-mail us if you like this guide, if you think that important material is omitted, if you encounter errors in the code examples or in the documentation, if you find any typos, or generally just if you feel like e-mailing. Send your email to Frank Brokken.

Please state the document version you're referring to, as found in the title (in this document: 5.2.0a) and please state the paragraph you're referring to.

All mail received is seriously considered, and new (sub)releases of the Annotations will normally reflect your suggestions for improvements. Except for the incidental case I will not otherwise acknowledge the receipt of suggestions for improvements. Please don't misinterpret this for lack of appreciation.

C++ offers a large number of facilities to implement solutions for common problems. Most of these facilities are part of the Standard Template Library or they are implemented as generic algorithms (see chapter 17).

Among the facilities C++ programmers have developed over and over again (as reflected in the Annotations) are those for manipulating chunks of text, commonly called strings. The C programming language offers rudimentary string support: the ASCII-Z terminated series of characters is the foundation on which a large amount of code has been built (We define an ASCII-Z string as a series of ASCII-characters terminated by the ASCII-character zero (hence -Z), which has the value zero, and should not be confused with character '0', which usually has the value 0x30).

Standard C++ now offers a string type of its own. In order to use string-type objects, the header file string must be included in sources.

Actually, string objects are class type variables, and the class is introduced for the first time in chapter 6. However, in order to use a string, it is not necessary to know what a class is. In this section the operators that are available for strings and some other operations are discussed. The operations that can be performed on strings take the form

stringVariable.operation(argumentList)

For example, if string1 and string2 are variables of type string, then

string1.compare(string2)
can be used to compare both strings. A function like compare(), which is part of the string-class is called a member function. The string class offers a large number of these member functions, as well as extensions of some well-known operators, like the assignment (=) and the comparison operator (==). These operators and functions are discussed in the following sections.

4.1: Operations on strings

Some of the operations that can be performed on strings return indices within the strings. Whenever such an operation fails to find an appropriate index, the value string::npos is returned. This value is a (symbolic) value of type string::size_type, which is (for all practical purposes) an int.

Note that in all operations with strings both string objects and char const * values and variables can be used.

Some string-member functions use iterators. Iterators will be covered in section 17.2. The member functions that use iterators are listed in the next section (4.2), they are not further illustrated below.

The following operations can be performed on strings:

4.2: Overview of operations on strings

In this section the available operations on strings are summarized. There are four subparts here: the string-initializers, the string-iterators, the string-operators and the string-member functions.

The member functions are ordered alphabetically by the name of the operation. Below, object is a string-object, and argument is either a string or a char const *, unless overloaded versions tailored to string and char const * parameters are explicitly mentioned. Object is used in cases where a string object is initialized or given a new value. Argument remains unchanged.

With member functions the types of the parameters are given in a function-prototypical way. With several member functions iterators are used. At this point in the Annotations it's a bit premature to discuss iterators, but for referential purposes they have to be mentioned nevertheless. So, a forward reference is used here: see section 17.2 for a more detailed discussion of iterators.

Finally, note that all string-member functions returning indices in object return the predefined constant string::npos if no suitable index could be found.

4.2.1: The string initializers

The following string constructors are available:

4.2.2: The string iterators

See section 17.2 for details about iterators.

4.2.3: The string operators

The following string operators are available:

4.2.4: The string member functions

The string member functions are listed in alphabetical order. The member name, prefixed by the string-class is given first. Then the full prototype and a description are given. Values of the type string::size_type represent index positions within a string. For all practical purposes, these values may be interpreted as int. The special value string::npos is defined to represent a non-existing index.

In the following overview, `size_type' should always be read as ` string::size_type'.