1. Intro
The std::string class comes with many built-in member functions that let you inspect and modify text without writing manual loops. These functions cover common tasks such as measuring length, extracting substrings, searching for characters or words, appending text, and safely accessing individual characters. Knowing the core set of string functions is essential for writing clean, exam-ready C++ code.
Cricket analogy: std::string member functions are like a scorer's toolkit: instead of manually counting every letter of a player's name, .length() and .find() handle common tasks like measuring and searching without writing loops.
2. Syntax
#include <string>
using namespace std;
string s = "Hello World";
s.length(); // or s.size() -> number of characters
s.substr(0, 5); // extract "Hello"
s.find("World"); // index where "World" starts, or string::npos
s.append("!"); // adds text to the end
s.at(1); // safe access to character at index 1
s.empty(); // true if length is 0
s.c_str(); // C-style const char* version of the string3. Explanation
.length() and .size() are equivalent and return the number of characters in the string. .substr(pos, len) returns a new string starting at index pos with up to len characters. .find(target) searches for target and returns its starting index, or the special constant string::npos if not found. .append(text) adds characters to the end of the string, similar to +=. .at(index) returns the character at a given position and throws an exception if the index is out of range, unlike operator[] which does not check bounds. .empty() is a quick way to test whether a string has zero length. .c_str() returns a null-terminated const char* view of the string, useful when interacting with C-style APIs.
Cricket analogy: "Virat Kohli".length() returns 11, .substr(6, 5) pulls out "Kohli", .find("Kohli") returns its starting index or string::npos if not found, .append(" (c)") marks him as captain, .at(0) safely gets 'V' with bounds checking unlike [0], .empty() checks for a blank scoresheet entry, and .c_str() converts the name for a legacy C-style API.
.at() performs bounds checking and throws std::out_of_range for an invalid index, while [] does not check bounds and causes undefined behavior on an invalid index. Prefer .at() when safety matters more than raw speed.
4. Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello World";
cout << "Length: " << s.length() << endl;
cout << "Substring: " << s.substr(6, 5) << endl;
cout << "Find 'World': " << s.find("World") << endl;
cout << "Is empty: " << (s.empty() ? "yes" : "no") << endl;
return 0;
}5. Output
Length: 11
Substring: World
Find 'World': 6
Is empty: no6. Key Takeaways
.length()/.size()return character count;.substr()extracts part of a string..find()returns the starting index of a match, orstring::nposwhen nothing is found..at()performs bounds checking and throws on invalid index, unlike[]..c_str()converts astd::stringto a null-terminated C-style string for interoperability.
Practice what you learned
1. What does `s.length()` return for a `std::string s`?
2. What does `s.find("xyz")` return if "xyz" is not found in `s`?
3. How does `.at(index)` differ from `operator[]` on a `std::string`?
4. What does `s.c_str()` return?
5. What does `s.substr(6, 5)` do on `s = "Hello World"`?
Was this page helpful?
You May Also Like
Strings in C++
Learn how the C++ `std::string` class represents text, why it is safer and more convenient than C-style char arrays, and how to declare and use it.
String Concatenation in C++
Learn how to join strings together in C++ using the + and += operators for std::string, and strcat() for C-style strings, along with safety concerns.
String Traversal in C++
Learn how to iterate through every character of a C++ string using index-based for loops and the range-based for loop, with practical examples.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics