string
The string
type supports the sequential container assignment operators and the assign
, insert
, and erase
operations (§ 9.2.5, p. 337, § 9.3.1, p. 342, and § 9.3.3, p. 348). It also defines additional versions of insert
and erase
.
In addition to the versions of insert
and erase
that take iterators, string
provides versions that take an index. The index indicates the starting element to erase
or the position before which to insert
the given values:
s.insert(s.size(), 5, '!'), // insert five exclamation points at the end of s
s.erase(s.size() - 5, 5); // erase the last five characters from s
The string
library also provides versions of insert
and assign
that take C-style character arrays. For example, we can use a null-terminated character array as the value to insert
or assign
into a string
:
const char *cp = "Stately, plump Buck";
s.assign(cp, 7); // s == "Stately"
s.insert(s.size(), cp + 7); // s == "Stately, plump Buck"
Here we first replace the contents of s
by calling assign
. The characters we assign into s
are the seven characters starting with the one pointed to by cp
. The number of characters we request must be less than or equal to the number of characters (excluding the null terminator) in the array to which cp
points.
When we call insert
on s
, we say that we want to insert the characters before the (nonexistent) element at s[size()]
. In this case, we copy characters starting seven characters past cp
up to the terminating null.
We can also specify the characters to insert
or assign
as coming from another string
or substring thereof:
string s = "some string", s2 = "some other string";
s.insert(0, s2); // insert a copy of s2 before position 0 in s
// insert s2.size() characters from s2 starting at s2[0] before s[0]
s.insert(0, s2, 0, s2.size());
append
and replace
FunctionsThe string
class defines two additional members, append
and replace
, that can change the contents of a string
. Table 9.13 summarizes these functions. The append
operation is a shorthand way of inserting at the end:
string s("C++ Primer"), s2 = s; // initialize s and s2 to "C++ Primer"
s.insert(s.size(), " 4th Ed."); // s == "C++ Primer 4th Ed."
s2.append(" 4th Ed."); // equivalent: appends " 4th Ed." to s2; s == s2
The replace
operations are a shorthand way of calling erase
and insert
:
// equivalent way to replace "4th" by "5th"
s.erase(11, 3); // s == "C++ Primer Ed."
s.insert(11, "5th"); // s == "C++ Primer 5th Ed."
// starting at position 11, erase three characters and then insert "5th"
s2.replace(11, 3, "5th"); // equivalent: s == s2
In the call to replace
, the text we inserted happens to be the same size as the text we removed. We can insert a larger or smaller string
:
s.replace(11, 3, "Fifth"); // s == "C++ Primer Fifth Ed."
In this call we remove three characters but insert five in their place.
string
The append
, assign
, insert
, and replace
functions listed Table 9.13 have several overloaded versions. The arguments to these functions vary as to how we specify what characters to add and what part of the string
to change. Fortunately, these functions share a common interface.
The assign
and append
functions have no need to specify what part of the string
is changed: assign
always replaces the entire contents of the string
and append
always adds to the end of the string
.
The replace
functions provide two ways to specify the range of characters to remove. We can specify that range by a position and a length, or with an iterator range. The insert
functions give us two ways to specify the insertion point: with either an index or an iterator. In each case, the new element(s) are inserted in front of the given index or iterator.
There are several ways to specify the characters to add to the string
. The new characters can be taken from another string
, from a character pointer, from a brace-enclosed list of characters, or as a character and a count. When the characters come from a string
or a character pointer, we can pass additional arguments to control whether we copy some or all of the characters from the argument.
Not every function supports every version of these arguments. For example, there is no version of insert
that takes an index and an initializer list. Similarly, if we want to specify the insertion point using an iterator, then we cannot pass a character pointer as the source for the new characters.
Exercise 9.43: Write a function that takes three string
s, s
, oldVal
, and newVal
. Using iterators, and the insert
and erase
functions replace all instances of oldVal
that appear in s
by newVal
. Test your function by using it to replace common abbreviations, such as “tho” by “though” and “thru” by “through”.
Exercise 9.44: Rewrite the previous function using an index and replace
.
Exercise 9.45: Write a funtion that takes a string
representing a name and two other string
s representing a prefix, such as “Mr.” or “Ms.” and a suffix, such as “Jr.” or “III”. Using iterators and the insert
and append
functions, generate and return a new string
with the suffix and prefix added to the given name.
Exercise 9.46: Rewrite the previous exercise using a position and length to manage the string
s. This time use only the insert
function.