← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 14 of 15

C++ String Methods

String methods are ready-made tools for text, like finding its length, searching inside, or cutting out a piece. They save you from looping through each letter yourself.
Syntax
cpp
string_name.length();
string_name.empty();
string_name.method_name(arguments);

Finding String Length

.length() and .size() are functionally identical and both return the exact number of characters currently stored in a std::string — .size() exists for consistency with other STL containers, while .length() reads a bit more naturally for strings specifically, but you can use either interchangeably.

Example: Finding String Length

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string s = "Hello";
	// Print to the console with cout
	std::cout << s.length() << " " << s.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Checking if Empty

.empty() returns true if a string has zero characters and false otherwise, and it's generally preferred over checking str.length() == 0, since it reads clearly as a direct yes/no question and can be marginally more efficient for some string implementations.

Example: Checking if Empty

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string s = "";
	// Print to the console with cout
	std::cout << (s.empty() ? "empty" : "not empty") << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Finding Substrings

.find(substring) searches the string for the first occurrence of a given substring and returns the index where it starts, or the special constant string::npos if no match exists anywhere in the string — always check the result against npos before using it, since a failed search doesn't throw an error, it just returns that sentinel value.

Example: Finding Substrings

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string s = "Hello, world";
	// Print to the console with cout
	std::cout << s.find("world") << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Modifying Strings

.append() adds text to the end of a string, .insert(pos, text) injects text at a specific index shifting the rest of the string over, and .erase(pos, count) removes a given number of characters starting at a position — together these give you full control over building and editing strings piece by piece.

Example: Modifying Strings

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string s = "Hello";
	s.append(", world");
	s.insert(0, ">> ");
	// Print to the console with cout
	std::cout << s << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Getting Substrings

.substr(start, length) extracts a copy of part of the string, starting at index start and taking up to length characters (or everything to the end if length is omitted) — the original string is left completely unchanged, since .substr() always returns a brand-new string rather than modifying in place.

Example: Getting Substrings

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>

// Program execution starts in main()
int main() {
	std::string s = "Hello, world";
	// Print to the console with cout
	std::cout << s.substr(7, 5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting that find returns string::npos if not found and comparing it with -1 incorrectly as an int.
  2. Using an index past the end with substr(pos), which throws out_of_range.
  3. Expecting s.length() to include the null terminator.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.