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

C++ String Length

The length() and size() member functions both return the number of characters in a std::string, and empty() checks whether a string has zero characters, without needing manual counting.
Syntax
cpp
variable_name.length();
variable_name.size();

The length() Method

The length() member function returns the number of characters currently stored in a std::string, not including any hidden terminator, since std::string manages its own size internally.

Example: The length() Method

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

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

The size() Method

size() returns exactly the same value as length() -- both exist because std::string implements the same interface as other STL containers, where size() is the conventional name.

Example: The size() Method

cpp
#include <iostream>
#include <string>

int main() {
	std::string name = "Alex";
	std::cout << name.size() << std::endl; // same value as length()
	return 0;
}

Checking for an Empty String

The empty() method returns true if a string has zero characters, which is a clearer and slightly more efficient way to check emptiness than comparing length() to 0.

Example: Checking for an Empty String

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

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

Using Length in a Loop

A string's length() is commonly used as the upper bound of a loop that visits each character by index, ensuring the loop never reads past the end of the string.

Example: Using Length in a Loop

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

// Program execution starts in main()
int main() {
	std::string word = "Hello";
	// Loop: repeat while the condition holds
	for (int i = 0; i < word.length(); i++) {
		// Print to the console with cout
		std::cout << word[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Length vs Capacity

A string's length() reflects how many characters it actually holds, while its internal capacity() -- the memory currently reserved -- can be larger, since std::string over-allocates to make future growth faster.

Example: Length vs Capacity

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

// Program execution starts in main()
int main() {
	std::string s = "Hi";
	// Print to the console with cout
	std::cout << "length=" << s.length() << " capacity=" << s.capacity() << 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. Writing s.length without the parentheses, which does not compile because length() is a member function.
  2. Looping with i <= s.length(), which goes one past the last character.
  3. Computing s.length() - 1 on an empty string, where the unsigned result wraps to a huge number.

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.