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.1.0d) 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:

  • Initialization: String objects can be initialized. For the initialization a plain ASCII-Z string, another string object, or an implicit initialization can be used. In the example, note that the implicit initialization does not have an argument, and may not use an argument list. Not even empty.
        #include <string>
        
        int main()
        {
            string
                stringOne("Hello World"),   // using plain ascii-Z
                stringTwo(stringOne),       // using another string object
                stringThree;                // implicit initialization to ""
                                            // do not use: stringThree();    
            return 0;
        }
    
  • Assignment: String objects can be assigned to each other. For this the assignment operator (i.e., the = operator) can be used, which accepts both a string object and a C-style characterstring as its right-hand argument:
        #include <string>
        
        int main()
        {
            string
                stringOne("Hello World"),
                stringTwo;                  
    
            stringTwo = stringOne;      // assign stringOne to stringTwo
            stringTwo = "Hello world";  // assign a C-string to StringTwo
    
            return 0;
        }
    
  • String to ASCII-Z conversion: In the previous example a standard C-string (an ASCII-Z string) was implicitly converted to a string-object. The reverse conversion (converting a string object to a standard C-string) is not performed automatically. In order to obtain the C-string that is stored within the string object itself, the member function c_str(), which returns a char const *, can be used:
        #include <string>
        
        int main()
        {
            string
                stringOne("Hello World");
            char const
                *Cstring = stringOne.c_str();
    
            cout << Cstring << endl;
    
            return 0;
        }
    
  • String elements: The individual elements of a string object can be reached for reading or writing. For this operation the subscript-operator ([]) is available, but there is no string pointer dereferencing operator (*). The subscript operator does not perform range-checking. String range checkingis done by the string::at() member function:
        #include <string>
        
        int main()
        {
            string
                stringOne("Hello World");
    
            stringOne[6] = 'w';         // now "Hello world"
            if (stringOne[0] == 'H')
                stringOne[0] = 'h';     // now "hello world"
    
            //  *stringOne = 'H';       // THIS WON'T COMPILE
    
            stringOne = "Hello World";  // Now using the at() 
                                        // member function:
            stringOne.at(6) = 
                    stringOne.at(0);    // now "Hello Horld"
            if (stringOne.at(0) == 'H')
                stringOne.at(0) = 'W';  // now "Wello Horld"
    
            return 0;
        }
    
    When an illegal index is passed to the at() member function, the program aborts.
  • Comparisons: Two strings can be compared for (in)equality or ordering, using the ==, !=, <, <=, > and >= operators or the string::compare() member function. The compare() member function comes in several flavors (see section 4.2.4 for details), e.g.:
  • int string::compare(string const &other): this variant offers a bit more information than the comparison-operators do. The return value of the string::compare() member function may be used for lexicographical ordering: a negative value is returned if the string stored in the string object using the compare() member function (in the example: stringOne) is located earlier in the ASCII collating sequence than the string stored in the string object passed as argument.
        #include <iostream>
        #include <string>
        
        using namespace std;
        
        int main()
        {
            string
                stringOne("Hello World"),
                stringTwo;                  
    
            if (stringOne != stringTwo)
                stringTwo = stringOne;
    
            if (stringOne == stringTwo)
                stringTwo = "Something else";
    
            if (stringOne.compare(stringTwo) > 0)
                cout <<
                "stringOne after stringTwo in the alphabet\n";
            else if (stringOne.compare(stringTwo) < 0)
                cout <<
                "stringOne before stringTwo in the alphabet\n";
            else
                cout << "Both strings are the same\n";
    
            // Alternatively:
    
            if (stringOne > stringTwo)
                cout <<
                "stringOne after stringTwo in the alphabet\n";
            else if (stringOne < stringTwo)
                cout <<
                "stringOne before stringTwo in the alphabet\n";
            else
                cout << "Both strings are the same\n";
    
            return 0;
        }
    
    Note that there is no member function to perform a case insensitive comparison of strings.
  • int string::compare(string const &other, string::size_type pos, unsigned n): the third argument indicates the number of characters that should be compared. If its value exceeds the number of available characters, only the available characters are compared.
  • More variants of string::compare() are available. As stated, refer to section 4.2.4 for details.
  • Appending: A string can be appended to another string. For this the += operator can be used, as well as the string &string::append() member function.

    Like the compare() function, the append() member function may have extra arguments. The first argument is the string to be appended, the second argument specifies the index position of the first character that will be appended. The third argument specifies the number of characters that will be appended. If the first argument is of type char const *, only a second argument may be specified. In that case, the second argument specifies the number of characters of the first argument that are appended to the string object. Furthermore, the + operator can be used to append two strings within an expression:

        #include <iostream>
        #include <string>
        
        using namespace std;
        
        int main()
        {
            string
                stringOne("Hello"),
                stringTwo("World");
    
            stringOne += " " + stringTwo;
            
            stringOne = "hello";
            stringOne.append(" world");
                                            // append 5 characters:
            stringOne.append(" ok. >This is not used<", 5);
    
            cout << stringOne << endl;
    
            string
                stringThree("Hello");
                                            // append " World":
            stringThree.append(stringOne, 5, 6);    
    
            cout << stringThree << endl;
        }
    
    The + operator can be used in cases where at least one term of the + operator is a string object (the other term can be a string, char const * or char).

    When neither operand of the + operator is a string, at least one operand must be converted to a string object first. An easy way to do this is to use an anonymous string object:

    string("hello") + " world";

  • Insertions: The string &string::insert() member function to insert (parts of) a string has at least two, and at most four arguments:
  • The first argument is the offset in the current string object where another string should be inserted.
  • The second argument is the string to be inserted.
  • The third argument specifies the index position of the first character in the provided string-argument that will be inserted.
  • The fourth argument specifies the number of characters that will be inserted.
  • If the first argument is of type char const *, the fourth argument is not available. In that case, the third argument indicates the number of characters of the provided char const * value that will be inserted.
        #include <string>
        
        int main()
        {
            string
                stringOne("Hell ok.");
                                // Insert "o " at position 4
            stringOne.insert(4, "o ");   
    
            string
                world("The World of C++");
    
                                // insert "World" into stringOne
            stringOne.insert(6, world, 4, 5);   
    
            cout << "Guess what ? It is: " << stringOne << endl;
        }
    
    Several variants of string::insert() are available. See section 4.2 for details.
  • Replacements: At times, the contents of string objects must be replaced by other information. To replace parts of the contents of a string object by another string the member function string &string::replace() can be used. The member function has at least three and possibly five arguments, having the following meanings (see section 4.2 for overloaded versions of replace(), using different types of arguments):
  • The first argument indicates the position of the first character that must be replaced
  • The second argument gives the number of characters that must be replaced.
  • The third argument defines the replacement text (a string or char const *).
  • The fourth argument specifies the index position of the first character in the provided string-argument that will be inserted.
  • The fifth argument can be used to specify the number of characters that will be inserted.
  • If the third argument is of type char const *, the fifth argument is not available. In that case, the fourth argument indicates the number of characters of the provided char const * value that will be inserted.

    The following example shows a very simple filechanger: it reads lines from cin, and replaces occurrences of a `searchstring' by a `replacestring'. Simple tests for the correct number of arguments and the contents of the provided strings (they should be unequal) are implemented using the assert() macro.

        #include <string>
        #include <cassert>
        
        int main(int argc, char **argv)
        {
            assert(argc == 3 && 
                "Usage: <searchstring> <replacestring> to process stdin");
                
            string
                line,
                search(argv[1]),    
                replace(argv[2]);
    
            assert(search != replace);
    
            while (getline(cin, line))
            {
                while (true)
                {
                    string::size_type
                        idx;
    
                    idx = line.find(search);
    
                    if (idx == string::npos)
                        break;
    
                    line.replace(idx, search.size(), replace);
                }                        
                cout << line << endl;
            }
            return 0;
        }
    
  • Swapping: A particular form of replacement is swapping: the member function string &string::swap(string &other) swaps the contents of two string-objects. For example:
        #include <string>
        
        int main()
        {
            string
                stringOne("Hello"),
                stringTwo("World");
            
            cout << "Before: stringOne: " << stringOne << ", stringTwo: "
                << stringTwo << endl;
    
            stringOne.swap(stringTwo);
    
            cout << "After: stringOne: " << stringOne << ", stringTwo: "
                << stringTwo << endl;
    
            return 0;
        }
    
  • Erasing: The member function string &string::erase() removes characters from a string. The standard form has two optional arguments:
  • If no arguments are specified, the stored string is erased completely: it becomes the empty string (string() or string("")).
  • The first argument may be used to specify the offset of the first character that must be erased.
  • The second argument may be used to specify the number of characters that are to be erased.
  • See section 4.2 for overloaded versions of erase(). An example of the use of erase() is given below:
        #include <string>
        
        int main()
        {
            string
                stringOne("Hello Cruel World");
    
            stringOne.erase(5, 6);
    
            cout << stringOne << endl;
    
            stringOne.erase();
    
            cout << "'" << stringOne << "'\n";
    
            return (0);
        }
    
  • Searching: To find substrings in a string the member function string::size_type string::find() can be used. This function looks for the string that is provided as its first argument in the string object calling find() and returns the index of the first character of the substring if found. If the string is not found string::npos is returned. The member function rfind() looks for the substring from the end of the string object back to its beginning. An example using find() was given earlier.
  • Substrings: To extract a substring from a string object, the member function string string::substr() is available. The returned string object contains a copy of the substring in the string-object calling substr() The substr() member function has two optional arguments:
  • Without arguments, a copy of the string itself is returned.
  • The first argument may be used to specify the offset of the first character to be returned.
  • The second argument may be used to specify the number of characters that are to be returned.
  • For example:
        #include <string>
        
        int main()
        {
            string
                stringOne("Hello World");
    
            cout << stringOne.substr(0, 5)  << endl
                 << stringOne.substr(6)     << endl
                 << stringOne.substr()      << endl;
        }
    
  • Character set searches: Whereas find() is used to find a substring, the functions find_first_of(), find_first_not_of(), find_last_of() and find_last_not_of() can be used to find sets of characters (Unfortunately, regular expressions are not supported here). The following program reads a line of text from the standard input stream, and displays the substrings starting at the first vowel, starting at the last vowel, and not starting at the first digit:
        #include <iostream>
        #include <string>
        
        int main()
        {
            string
                line;
    
            getline(cin, line);
    
            string::size_type
                pos;
    
            cout << "Line: " << line << endl 
                << "Starting at the first vowel:\n"
                << "'" 
                    << (
                        (pos = line.find_first_of("aeiouAEIOU")) 
                        != string::npos ?
                            line.substr(pos) 
                        : 
                            "*** not found ***"
                        ) << "'\n"
                << "Starting at the last vowel:\n"
                << "'" 
                    << (
                        (pos = line.find_last_of("aeiouAEIOU")) 
                        != string::npos ?
                            line.substr(pos) 
                        : 
                            "*** not found ***"
                        ) << "'\n"
                << "Not starting at the first digit:\n"
                << "'" 
                    << (
                        (pos = line.find_first_not_of("1234567890"))
                        != string::npos ?
                            line.substr(pos) 
                        : 
                            "*** not found ***"
                        ) << "'\n";
        }
    
  • String size: The number of characters that are stored in a string are obtained by the size() member function, which, like the standard C function strlen() does not include the terminating ASCII-Z character. For example:
        #include <string>
        
        int main()
        {
            string
                stringOne("Hello World");
    
            cout << "The length of the stringOne string is " 
                << stringOne.size() << " characters\n";
    
            return 0;
        }
    
  • Empty strings: The size() member function can be used to determine whether a string holds no characters. Alternatively, the string::empty() member function can be used:
        #include <string>
        
        int main()
        {
            string
                stringOne;
    
            cout << "The length of the stringOne string is " 
                << stringOne.size() << " characters\n" 
                    "It is " << (stringOne.empty() ? "" : " not ") 
                << "empty\n";
    
            stringOne = "";
            
            cout << "After assigning a \"\"-string to a string-object\n"
                    "it is " << (stringOne.empty() ? "also" : " not") 
                << " empty\n";
            
            return 0;
        }
    
  • Resizing strings: If the size of a string is not enough (or if it is too large), the member function void string::resize() can be used to make it longer or shorter. Note that operators like += automatically resize a string when needed.
  • Reading a string from a stream: The istream &getline(istream) tt(&instream, string &target, char delimiter) member function may be used to read a line of text (up to the first delimiter or the end of the stream) from instream.

    The delimiter has a default value '\n'. It is removed from instream, but it is not stored in target. Istream::fail() may be called to determine whether the delimiter was found. If it returns true the delimiter was not found (see chapter 5 for details about istream objects). The function getline() was used in several earlier examples (e.g., with the replace() member function).

  • 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:
  • string object:
    Initializes object to an empty string.
  • string object(string::size_type n, char c):
    Initializes object with n characters c.
  • string object(string argument):
    Initializes object with argument.
  • string object(string argument, string::size_type idx,) tt(string::size_type n = pos):
    Initializes object with argument, using n characters of argument, starting at index idx.
  • string object(InputIterator begin, InputIterator end):
    Initializes object with the range of characters implied by the provided InputIterators.
  • 4.2.2: The string iterators

    See section 17.2 for details about iterators.
  • Forward iterators returned by the members:
  • string::begin()
  • string::end()
  • Reverse iterators returned by the members:
  • string::rbegin()
  • string::rend()
  • 4.2.3: The string operators

    The following string operators are available:
  • object = argument.
    Assignment of argument to object. May also be used for initializing string objects.
  • object = c.
    Assignment of char c to object. May not be used for initializing string objects.
  • object += argument.
    Appends argument to object. Argument may also be a char value.
  • argument1 + argument2.
    Within expressions, strings may be added. At least one term of the expression (the left-hand term or the right-hand term) should be a string object. The other term may be a string, a char const * value or a char value, as illustrated by the following example:
        void fun()
        {
            char const 
                *asciiz = "hello";
            string
                first = "first",
                second;
    
                // all expressions compile ok:
            second = first + asciiz;  
            second = asciiz + first;  
            second = first + 'a';
            second = 'a' + first;
        }
    
  • object[string::size_type pos].
    The subscript-operator may be used to assign individual characters of object or to retrieve these characters. There is no range-checking. If range checking is required, use the at() member function, summarized earlier.
  • argument1 == argument2.
    The equality operator ( operator==()) may be used to compare a string object to another string or char const * value. The operator!=() is available as well. The return value for both is a bool. For two identical strings operator==() returns true, and operator!=() returns false.
  • argument1 < argument2.
    The less-than operator may be used to compare the ordering within the Ascii-character set of argument1 and argument2. The operators <=, > and >= are available as well.
  • ostream stream; stream << argument.
    The insertion-operator may be used with string objects.
  • istream stream; stream >> object.
    The extraction-operator may be used with string objects. It operates analogously to the extraction of characters into a character array, but object is automatically resized to the required number of characters.
  • 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.
  • char &string::at(size_type pos):
    The character (reference) at the indicated position is returned (it may be reassigned). The member function performs range-checking, aborting the program if an invalid index is passed.
  • string &string::append(InputIterator begin, InputIterator end):
    Using this member function the range of characters implied by the begin and end InputIterators are appended to the string object.
  • string &string::append(string argument, size_type pos, size_type n):
  • If only argument is given, it is appended to the string object.
  • If pos is specified as well, argument is appended from index position pos until the end of argument.
  • If all three arguments are provided, n characters of argument, starting at index position pos are appended to the string object.
  • If argument is of type char const *, parameter pos cannot be specified. So, with char const * arguments, either all characters or an initial subset of the characters of the provided char const * argument are appended to the string object.
  • string &string::append(size_type n, char c):
    Using this member function, n characters c can be appended to the string object.
  • string &string::assign(string argument, size_type pos, size_type n):
  • If only argument is given, it is assigned to the string object.
  • If pos is specified as well, the string object is assigned from index position pos until the end of argument.
  • If all three arguments are provided, n characters of argument, starting at index position pos are assigned to the string object.
  • If argument is of type char const *, no parameter pos is available. So, with char const * arguments, either all characters or an initial subset of the characters of the provided char const * argument are assigned to the string object.
  • string &string::assign(size_type n, char c): Using this member function, n characters c can be assigned to the string object.
  • size_type string::capacity():
    returns the number of characters that can currently be stored inside the string object.
  • int string::compare(string argument):
    This member function can be used to compare (according to the ASCII-character set) the text stored in the string object and in argument. The argument may also be a (non-0) char const *. 0 is returned if the characters in the string object and in argument are the same; a negative value is returned if the text in string is lexicographically before the text in argument; a positive value is returned if the text in string is lexicographically beyond the text in argument.
  • int string::compare(size_type idx, size_type len, string argument):
    This member function can be used to compare a substring of the text stored in the string object with the text stored in argument. At most len characters, starting at offset idx, are compared with the text in argument. If idx is or exceeds the number of characters in the string object, an (out_of_range) exception is thrown (see chapter 8). The argument may also be a (non-0) char const *.
  • int string::compare(size_type idx, size_type len, string argument, size_type arg_idx, size_type arg_len):
    This member function can be used to compare a substring of the text stored in the string object with a substring of the text stored in argument. At most len characters of the string object, starting at offset idx, are compared with at most arg_len characters of argument, starting at offset arg_idx. If idx or arg_idx is or exceeds the number of characters in their respective string objects, an (out_of_range) exception is thrown. Note that argument must also be a string object.
  • int string::compare(size_type idx, size_type len, char const *argument, size_type arg_len):
    This member function can be used to compare a substring of the text stored in the string object with a substring of the text stored in argument. At most len characters of the string object, starting at offset idx, are compared with at most arg_len characters of argument. If idx is or exceeds the number of characters in the tt (string) object, an (out_of_range) exception is thrown. Argument must have at least arg_len characters. However, the characters may have arbitrary values: the ASCII-Z value has no special meaning.
  • size_type string::copy(char const *argument, size_type n, size_type pos):
    If the third argument is omitted, the first n characters of the string object are copied to argument. If the third argument is given, copying starts from element pos of the string object. Following the copying, no ASCII-Z is appended to the copied string. If n exceeds the string object's length(), at most length() characters are copied. The actual number of characters that were copied is returned.
  • char const *string::c_str():
    the member function returns the contents of the string object as an ASCII-Z C-string.
  • char const *string::data():
    returns the raw text stored in the string object.
  • bool string::empty():
    returns true if the string object contains no data.
  • string &string::erase(size_type pos; size_type n):
    This member function can be used to erase (a sub)string of the string object. The basic form erases the string object completely. The working of other forms of erase() depend on the specification of extra arguments:
  • If pos is specified, the contents of the string object are erased from index position pos until the end of the string object.
  • If pos and n are provided, n characters of the string object, starting at index position pos are erased.
  • iterator string::erase(iterator p):
    The contents of the string object are erased until (iterator) position p. The iterator p is returned.
  • iterator string::erase(iterator f, iterator l):
    The range of characters of the string object, implied by the iterators f and l are erased. The iterator f is returned.
  • size_type string::find(string argument, size_type pos):
    Returns the index in the string object where argument is found. If pos is omitted, the search starts at the beginning of the string object. If pos is provided, it refers to the index in the string object where the search for argument should start.
  • size_type string::find(char const *argument, size_type pos, size_type n):
    Returns the index in the string object where argument is found. The parameter n indicates the number of characters of argument that should be used in the search: it defines a partial string starting at the beginning of argument. If omitted, all characters in argument are used. The parameter pos refers to the index in the string object where the search for argument should start. If the parameter pos is omitted as well, the string object is scanned completely.
  • size_type string::find(char c, size_type pos):
    Returns the index in the string object where c is found. If the argument pos is omitted, the search starts at the beginning of the string object. If provided, it refers to the index in the string object where the search for the string object should start.
  • size_type string::find_first_of(string argument, size_type pos):
    Returns the index in the string object where any character in argument is found. If the argument pos is omitted, the search starts at the beginning of the string object. If provided, it refers to the index in the string object where the search for argument should start.
  • size_type string::find_first_of(char const* argument, size_type pos, size_type n):
    Returns the index in the string object where a character of argument is found, no matter which character. The parameter n indicates the number of characters of the string object that should be used in the search: it defines a partial string starting at the beginning of the string object. If omitted, all characters in the string object are used. The parameter pos refers to the index in the string object where the search for argument should start. If the parameter pos is omitted as well, the string object is scanned completely.
  • size_type string::find_first_of(char c, size_type pos):
    Returns the index in the string object where character c is found. If the argument pos is omitted, the search starts at the beginning of the string object. If provided, it refers to the index in the string object where the search for c should start.
  • size_type string::find_first_not_of(string argument, size_type pos):
    Returns the index in the string object where a character not appearing in argument is found. If the argument pos is omitted, the search starts at the beginning of the string object. If provided, it refers to the index in the string object where the search for argument should start.
  • size_type string::find_first_not_of(char const *argument, size_type pos, size_type n):
    Returns the index in the string object where any character not appearing in argument is found. The parameter n indicates the number of characters of the string object that should be used in the search: it defines a partial string starting at the beginning of the string object. If omitted, all characters in the string object are used. The parameter pos refers to the index in the string object where the search for argument should start. If the parameter pos is omitted as well, the string object is scanned completely.
  • size_type string::find_first_not_of(char c, size_type pos):
    Returns the index in the string object where another character than c is found. If the argument pos is omitted, the search starts at the beginning of the string object. If provided, it refers to the index in the string object where the search for c should start.
  • size_type string::find_last_of(string argument, size_type pos):
    Returns the last index in the string object where a character in argument is found. If the argument pos is omitted, the search starts at the beginning of the string object. If provided, it refers to the index in the string object where the search for argument should start.
  • size_type string::find_last_of(char const* argument, size_type pos, size_type n):
    Returns the last index in the string object where a character of argument is found. The parameter n indicates the number of characters of the string object that should be used in the search: it defines a partial string starting at the beginning of the string object. If omitted, all characters in the string object are used. The parameter pos refers to the index in the string object where the search for argument should start. If the parameter pos is omitted as well, the string object is scanned completely.
  • size_type string::find_last_of(char c, size_type pos):
    Returns the last index in the string object where character c is found. If the argument pos is omitted, the search starts at the beginning of the string object. If provided, it refers to the index in the string object where the search for c should start.
  • size_type string::find_last_not_of(string argument, size_type pos):
    Returns the last index in the string object where any character not appearing in argument is found. If the argument pos is omitted, the search starts at the beginning of the string object. If provided, it refers to the index in the string object where the search for argument should start.
  • size_type string::find_last_not_of(char const *argument, size_type pos, size_type n):
    Returns the last index in the string object where any character not appearing in argument is found. The parameter n indicates the number of characters of the string object that should be used in the search: it defines a partial string starting at the beginning of the string object. If omitted, all characters in the string object are used. The parameter pos refers to the index in the string object where the search for argument should start. If the parameter pos is omitted as well, all of the string object is scanned.
  • size_type string::find_last_not_of(char c, size_type pos):
    Returns the last index in the string object where another character than c is found. If the argument pos is omitted, the search starts at the beginning of the string object. If provided, it refers to the index in the string object where the search for c should start.
  • istream &getline(istream instream, string object, char delimiter):
    This member function can be used to read a line of text (up to the first delimiter or the end of the stream) from instream. The delimiter has a default value '\n'. It is removed from instream, but it is not stored in the string object.
  • string &string::insert(size_type t_pos, string argument, size_type pos; size_type n):
    This member function can be used to insert (a sub)string of argument into the string object, at the string object's index position t_pos. The basic form inserts argument completely at index t_pos. The way other forms of insert() work depend on the specification of extra arguments:
  • If pos is specified, argument is inserted from index position pos until the end of argument.
  • If pos and n are provided, n characters of argument, starting at index position pos are inserted into the string object.
  • If argument is of type char const *, no parameter pos is available. So, with char const * arguments, either all characters or an initial subset of the characters of the provided char const * argument are inserted into the string object.
  • string &string::insert(size_type t_pos, size_type n, char c): Using this member function, n characters c can be inserted to the string object.
  • iterator string::insert(iterator p, char c):
    The character c is inserted at the (iterator) position p in the string object. The iterator p is returned.
  • iterator string::insert(iterator p, size_type n, char c):
    N characters c are inserted at the (iterator) position p in the string object. The iterator p is returned.
  • iterator string::insert(iterator p, InputIterator first, InputIterator last):
    The range of characters implied by the InputIterators first and last are inserted at the (iterator) position p in the string object. The iterator p is returned.
  • size_type string::length():
    returns the number of characters stored in the string object.
  • size_type string::max_size():
    returns the maximum number of characters that can be stored in the string object.
  • string& string::replace(size_type pos1, size_type n1, const string argument, size_type pos2, size_type n2):
    The substring of n1 characters of the string object, starting at position pos1 is replaced by argument. If n1 is set to 0, the member function inserts argument into the string object.
    The basic form uses argument completely. The way other forms of replace() work depends on the specification of extra arguments:
  • If pos2 is specified, argument is inserted from index position pos2 until the end of argument.
  • If pos2 and n2 are provided, n2 characters of argument, starting at index position pos2 are inserted into the string object.
  • If argument is of type char const *, no parameter pos2 is available. So, with char const * arguments, either all characters or an initial subset of the characters of the provided char const * argument are replaced in the string object.
  • string &string::replace(size_type pos, size_type n1, size_type n2, char c):
    This member function can be used to replace n1 characters of the string object, starting at index position pos, by n2 c-characters. The argument n2 may be omitted, in which case the string to be replaced is replaced by just one character c.
  • string& string::replace (iterator i1, iterator i2, string argument):
    Here, the string implied by the iterators i1 and i2 are replaced by the string str. If argument is a char const *, an extra argument n may be used, specifying the number of characters of argument that are used in the replacement.
  • iterator string::replace(iterator f, iterator l, string argument):
    The range of characters of the string object, implied by the iterators f and l are replaced by argument. If argument is a char const *, an extra argument n may be used, specifying the number of characters of argument that are used in the replacement. The string the string object is returned.
  • iterator string::replace(iterator f, iterator l, size_type n, char c):
    The range of characters of the string object, implied by the iterators f and l are replaced by n c-characters. The iterator f is returned.
  • string string::replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2):
    Here the range of characters implied by the iterators i1 and i2 is replaced by the range of characters implied by the InputIterators j1 and j2.
  • void string::resize(size_type n, char c):
    The string stored in the string object is resized to n characters. The second argument is optional. If provided and the string is enlarged, the extra characters are initialized to c.
  • size_type string::rfind(string argument, size_type pos):
    Returns the index in the string object where argument is found. Searching proceeds either from the end of the string object or from offset pos back to the beginning. If the argument pos is omitted, searching starts at the end of the string object. If pos is provided, it refers to the index in the string object where the search for argument should start.
  • size_type string::rfind(char const *argument, size_type pos, size_type n):
    Returns the index in the string object where argument is found. Searching proceeds either from the end of the string object or from offset pos back to the beginning. The parameter n indicates the number of characters of argument that should be used in the search: it defines a partial string starting at the beginning of argument. If omitted, all characters in argument are used. If the argument pos is omitted as well, searching starts at the end of the string object. If pos is provided, it refers to the index in the string object where the search for (a substring of) argument should start.
  • size_type string::rfind(char c, size_type pos):
    Returns the index in the string object where c is found. Searching proceeds either from the end of the string object or from offset pos back to the beginning.
  • size_type string::size():
    returns the number of characters stored in the string object.
  • string string::substr(size_type pos, size_type n):
    Returns a substring of the string object. The parameter n may be used to specify the number of characters of argument that are returned. The parameter pos may be used to specify the index of the first character of argument that is returned. Either n or both arguments may be omitted.
  • size_type string::swap(string argument):
    swaps the contents of the string object and argument. In this case, argument must be a string and cannot be a char const *.