C++ String Length
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
#include <string>
int main() {
std::string name = "Alex";
std::cout << name.size() << std::endl; // same value as length()
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Writing
s.lengthwithout the parentheses, which does not compile becauselength()is a member function. - Looping with
i <= s.length(), which goes one past the last character. - Computing
s.length() - 1on an empty string, where the unsigned result wraps to a huge number.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings