std::string is the C++ standard-library string type declared in <string>. It manages its character count and storage automatically, making it the usual choice for text instead of manually maintaining a null-terminated character array.
Create, read, and print
A string can be initialized from text and changed by assignment. cin >> text reads one word; use getline(cin, text) when spaces must be preserved.
string language = "C++";
string topic;
getline(cin, topic);
cout << language << ": " << topic << endl;Length and state
size() and length() both return the number of characters as size_t. empty() reports whether the string contains no characters. None of these operations changes the string.
string text = "Leaflet";
cout << text.size() << endl; // 7
cout << text.length() << endl; // 7
cout << boolalpha << text.empty() << endl; // falseAccess individual characters
text[index] accesses a character with a zero-based index. front() and back() access the first and final characters. at(index) also uses an index but performs bounds checking.
string text = "Leaflet";
cout << text[0] << endl; // L
cout << text.front() << endl; // L
cout << text.back() << endl; // t
text[0] = 'l';Append and insert
append(value) and += add text at the end. insert(position, value) inserts text before a given index. These methods modify the original string.
string text = "Leaf";
text.append("let"); // Leaflet
text += " course"; // Leaflet course
text.insert(7, " C++"); // Leaflet C++ courseExtract, find, and edit
substr(start, count) returns a new string. find(value) returns the starting index of the first match, or string::npos if no match exists. replace(start, count, value) replaces a range, while erase(start, count) removes one.
string text = "Leaflet course";
string name = text.substr(0, 7); // Leaflet
size_t position = text.find("course"); // 8
if (position != string::npos) {
text.replace(position, 6, "lab"); // Leaflet lab
}
text.erase(7, 4); // LeafletC++ string method workshop
Choose a method to inspect its call, whether it modifies the original string, and its returned or edited character layout.
text.size();Returns the character count; the space counts too.
Compare strings
== and != test content equality. compare() returns a negative value, zero, or a positive value when the current string sorts before, equals, or sorts after its argument.
string first = "apple";
string second = "banana";
if (first != second && first.compare(second) < 0) {
cout << first << " comes first" << endl;
}KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Build a username
Read a one-word first and last name, build the std::string username “first.last”, and print it together with its length.
- Read first and last separately with cin.
- Concatenate first + '.' + last.
- Print the username and username.size().
Extract an email domain
Read an email-shaped string and extract the domain after @, reporting an error when @ is absent or the domain is empty.
- Use find to locate @.
- Check for string::npos and ensure characters follow @.
- Use substr(position + 1) and print the domain.
Perform one text replacement
Read source text, a search fragment, and a replacement, then replace only the first occurrence.
- Use find to obtain the first match position.
- When absent, keep the source unchanged and report it.
- When found, call replace(position, old_text.size(), new_text) and print the result.