C++ String Methods
C++ string methods are built-in functions you call on a std::string value. They help you inspect, search, extract, and change text without writing all the character-by-character logic yourself.
You call a method with dot syntax, such as word.length() or message.find("C++").
Checking Size
Use length() or size() to count characters in a string. Use empty() to check whether a string has no characters.
#include <iostream>
#include <string>
int main(void) {
std::string word = "compiler";
std::string blank = "";
std::cout << "Word: " << word << std::endl;
std::cout << "Length: " << word.length() << std::endl;
std::cout << "Size: " << word.size() << std::endl;
std::cout << "Blank is empty: " << blank.empty() << std::endl;
return 0;
}
Output:
Word: compiler
Length: 8
Size: 8
Blank is empty: 1
For std::string, length() and size() return the same value. The empty() method returns a Boolean value; when printed with std::cout, true appears as 1 and false appears as 0.
Searching A String
Use find() to search for text inside a string. It returns the index where the match begins. If the text is not found, it returns the special value std::string::npos.
#include <iostream>
#include <string>
int main(void) {
std::string fileName = "notes-final.txt";
std::size_t finalPos = fileName.find("final");
std::size_t draftPos = fileName.find("draft");
std::cout << "final position: " << finalPos << std::endl;
if (draftPos == std::string::npos) {
std::cout << "draft was not found" << std::endl;
}
return 0;
}
Output:
final position: 6
draft was not found
String indexes start at 0, so final starts at index 6 in "notes-final.txt". Searches are case-sensitive: find("Final") would not match "final".
Extracting Part Of A String
The substr() method returns a new string made from part of another string. The form substr(start, count) starts at start and copies up to count characters.
#include <iostream>
#include <string>
int main(void) {
std::string code = "CPP-2026-BASICS";
std::string topic = code.substr(0, 3);
std::string year = code.substr(4, 4);
std::string level = code.substr(9);
std::cout << "Topic: " << topic << std::endl;
std::cout << "Year: " << year << std::endl;
std::cout << "Level: " << level << std::endl;
return 0;
}
Output:
Topic: CPP
Year: 2026
Level: BASICS
If you call substr() with only the starting index, it returns everything from that index to the end of the string.
Changing A String
Unlike many string operations in some other languages, several C++ string methods change the original string. Common methods include append(), insert(), erase(), and replace().
| Method | What it does |
|---|---|
append(text) |
Adds text to the end. |
insert(index, text) |
Adds text before an index. |
erase(index, count) |
Removes characters. |
replace(index, count, text) |
Replaces characters with new text. |
#include <iostream>
#include <string>
int main(void) {
std::string title = "C++ Strings";
title.append(" Course");
title.insert(0, "Learn ");
title.replace(6, 3, "Modern C++");
title.erase(16, 8);
std::cout << title << std::endl;
return 0;
}
Output:
Learn Modern C++ Course
In this example, each method updates title. The indexes used by insert(), replace(), and erase() are zero-based.
Common Mistakes
- Forgetting to include
<string>before usingstd::string. - Checking
find()against0instead ofstd::string::nposwhen text is not found. - Mixing up
substr(start, count)with an ending index. The second value is a character count. - Using indexes that are outside the string.
Takeaway: C++ string methods let you check, search, extract, and update text while keeping your code short and readable.
