Note that if you use append on a string, that string will replace the old string.
Notice how the first value of string is "Willkommen,", and after you appended the string, the new string value is Willkommen, world!.
Length
In order to retrieve the length of string, you use string.length() String length is the number of characters in a particular string.
string example = "1234567890"cout << "The length of the string is " << example.length();
If you count the number of characters there are in the string, it’s like:
1
2
3
4
5
6
7
8
9
10
1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
An alternative method of getting the length of a string is string.size()
Accessing Characters
To retrieve a letter in the string, you can reference a specific index of the string. string is a sequence of characters so the first index of the sequence is 0.
Index
0
1
2
3
4
Character
h
e
l
l
o
string myString = "hello";cout << myString[0];// Outputs h
If you want to get the last letter of the string, simply use string.length() and subtract 1 (if you recall correctly about the first index, which is 0)
string myString = "hello";cout << myString[myString.length() - 1];// Outputs o
at() function
A similar function of accessing characters is string.at() Using the previous example,
To modify a specific char in the string, you refer back to the index number of the string and assign it to a new character similar to reassigning a value to a char variable.
string myString = "Cat";myString[0] = "K";cout << myString;// Outputs Kat
Finding a substring
A substring is a portion of the string. To search a substring, you use string.find()
#include <iostream>#include <string>using namespace std;int main() { string text = "Coding is fun!"; string keywords = "fun"; size_t keywordsFoundAt = text.find(keywords); if (keywordsFoundAt != string::npos) cout << "Found the keywords at index " << keywordsFoundAt << endl; return 0;}
In the example, the function finds the keywords of the text at index 11. If we had set keywords differently to anything or any string that isn’t found in the text, keywordsFoundAt returns string::npos, which is also the same as -1.
To extract a substring, you use string.substr(). The first argument of the string.substr() is the index number of the string (7) and the second argument is the length after the index (5).
#include <iostream>#include <string>using namespace std;int main() { string text = "Hello World!"; string sub = text.substr(7, 5); cout << "Substring: " << sub; // Substring: World return 0;}
In the example, we specified 7 to start the extraction from index 7, which is the 8th character of the string, and plus the length of the extraction 5. The extraction length starts on the index you specified, not one after the index.
Converting characters to UPPERCASE
To convert characters into uppercase, use toupper(). Before using that function, you must include a library cctype.
#include <cctype>
toupper() and tolower() with strings
These two functions don’t accept string in the first argument. You must specify a character!!
Here’s an example:
#include <iostream>#include <string>#include <cctype>using namespace std;int main() { string what = "Hello World!"; int i; for (i = 0; i < what.length(); i++) { what[i] = toupper(what[i]); }; cout << what; // Outputs HELLO WORLD! return 0;}
Lowercase
An alternative method is tolower() similar to toupper() but changing all letters to lowercase.
#include <iostream>#include <string>#include <cctype>using namespace std;int main() { string what = "Hello World!"; int i; for (i = 0; i < what.length(); i++) { what[i] = tolower(what[i]); }; cout << what; // Outputs hello world! return 0;}