The string
Type
Chapter 7
Introduction
To use the data type,
string
, a program must include the header filestring
.A
string
is a sequence of 0 or more characters- The first character is in position
0
- The second character is in position
1
, etc.
- The first character is in position
Binary operator + performs the string concatenation operation
The statement:
cppstring str1 = "Hello There";
stores the string "Hello There" in str1
.
The statement:
cppstring str2 = str1;
copies the value of
str1
intostr2
.If
str1 == "Sunny"
, the statement:cppstr2 = str1 + " Day";
stores the string "Sunny Day" into
str2
.Suppose
str1 == "Hello"
andstr2 == "There"
. The statement:cppstr3 = str1 + " " + str2;
stores "Hello There" into
str3
. This statement is equivalent to the statement:cppstr3 = str1 + ' ' + str2;
Also, the statement:
cppstr3 += ", Mickey";
updates the value of
str3
by appending thestring
", Mickey" to its old value. Therefore, the new value ofstr3
is "Hello There, Mickey".
The Array-Subscript Operator
Array-subscript operator []
allows access to an individual character in a string
.
string str1 = "know";
str1[0] == 'k'; // true
str1[3] == 'w'; // true
str1[0] = 's';
cout << str1 << endl; // snow
The number inside of the array subscript operator []
is called the index of the character.
A string's size (the number of characters it can hold) is larger than the maximum value of an
int
orunsigned int
. Therefore, a new data type,string::size_type
, should be used.string::size_type
An unsigned integer (data) type. This is like anint
but guaranteed to be big enough to hold astring
of any size. (Very useful for looping over the length of astring
!) Unsigned means it cannot hold negative numbers.string::npos
The maximum value of the (data) typestring::size_type
. This number is 4,294,967,295 (i.e.,) on many machines.
string::size_type index = 0; string str1 = "know"; str1[index] == 'k'; // true
Example of iterating over each character in a
string
:// Display a string with a space between each character. for (string::size_type index = 0; index < str1.length(); ++index) { cout << str1[index] << ' '; }
Range-Based Loops
An easier way to iterate!
The string
is a data type that contains a range of values (unlike the other types discussed, which each hold a single value). We can use a new type of loop, the range-based for loop, to iterate over each char
in a string
.
The following example code displays, H e l l o , w o r l d!
.
const string MESSAGE = "Hello, world!\n";
for (char letter : MESSAGE) {
cout << letter << ' ';
}
For each iteration of the loop in the example above, letter
gets a copy of the next character in MESSAGE
.
You may also decare the loop variable as a reference if you want to edit the string.
The following example changes all of the values to be the next letter in the alphabet.
string greeting = "Hiya";
for (char& letter : greeting) {
++letter;
}
cout << greeting << endl; // Displays, "Ijzb"
TIP
Use a range-based for loop to iterate over all strings if the following conditions are met:
- You don't need the index (we don't need to know where the character is in the string).
- You are iterating over the whole string from the first to last character.
Additional string
Operations
Swap Function
cppstring str1 = "Warm"; string str2 = "Cold"; str1.swap(str2);
Now,
str1
is "Cold" andstr2
is "Warm".The
substr()
member function the returns portion of a string from a starting index. See the following table for details.cppstring sentence = "Houston, we have a problem."; cout << sentence.substr(0, 7) << endl; // Houston cout << sentence.substr(9, 2) << endl; // we
The table below has many other useful
string
member functions.The
find()
member function returns the position (index) of the parameter value within the string. If the value doesn't exist within the string, the constantstring::npos
is returned.cppconst string PHIL_4_13 = "I can do all things through him who strengthens me."; // ESV auto loc = PHIL_4_13.find("all"); // 9 cout << PHIL_4_13.substr(loc, 2) << endl; // do
Expression | Effect |
---|---|
str1.at(index) | Returns the element at the position specified by index . |
str1[index] | Returns the element at the position specified by index . |
str1.append(num, ch) | Appends num copies of ch to str1 , in which ch is a char variable or a char constant. |
str1.append(str2) | Appends str2 to str1 . |
str1.clear() | Deletes all the characters in str1 . |
str1.compare(str2) | returns 1 if str1 > str2 ; returns 0 if str1 == str2; returns -1 if str1 < str2 . |
str1.empty() | Returns true if str1 is empty; otherwise, it returns false . |
str1.erase() | Deletes all the characters in str1 . |
str1.erase(pos, num) | Deletes num characters from str1 starting at position pos . |
str1.find(str2) | Returns the index of the first occurrence of str2 in str1 . If str2 is not found, the special value string::npos is returned. |
str1.find(str2, pos) | Returns the index of the first occurrence at or after pos where str2 is found in str1 . |
str1.find_first_of(str2, pos) | Returns the index of the first occurrence of any character of str1 in str2 . The search starts at pos . |
str1.find_first_not_of(str2, pos) | Returns the index of the first occurrence of any character of str2 not in str1 . The search starts at pos . |
str1.insert(pos, num, ch); | Inserts num occurrences of the character ch at index pos into str1 ; pos and num are of type string::size_type ; ch is a character. |
str1.insert(pos, str2); | Inserts all the characters of str2 at index pos into str1 . |
str1.length() | Returns a value of type string::size_type giving the number of characters str1 . Equivalent to str1.size() |
str1.replace(pos, num, str2); | Starting at index pos , replaces the next num characters of str1 with all the characters of str2 . If num > the length of str1 , all the characters until the end of str1 are replaced. |
str1.substr(pos, len) | Returns a substring of str1 starting at pos . The length of the substring is at most len characters. If len is too large, it means "to the end" of the string in str1 . |
str1.size() | Returns a value of type string::size_type giving the number of characters str1. Equivalent to str1.length() . |
str1.swap(str2); | Swaps the contents of str1 and str2 . |