Skip to Content

C++: The C++ std::string Class

Plain C Strings are described elsewhere. This page discusses the C++ std::string class, which can (and should) be used for programming with strings in C++. This is a simple presentation of only the basics; for a full reference you can go to CPPReference.com::strings.

Plain C strings are simply arrays of characters, ending with a ‘\0’ (null) character. The C Standard Library includes many functions for manipulating these strings. As simple arrays, C strings do not know anything about themselves, nor can they be managed very well.

The std::string class is in the C++ Standard Library. Objects of this class represent strings, but as objects they also know about themselves (such as their length), and have methods that can manipulate themselves.

Many sources will tell you to use the statement using namespace std; in your program, and then just use string as the name of the class. Do not do this. Here is just one reference on why this is bad.

Constructing

Remember that in C++ you can declare objects directly, and pointers to objects. So

std::string  str1;
std::string* str2;

declares a string object named str1 and a pointer to a string object named str2. Only one object exists so far. We need to do

str2 = new std::string();

to create a new string object and make str2 point to it.

The examples above create objects that have empty strings as their data. You can create one that has an initial string by giving the string to the constructor:

std::string  str1("This is string one");
std::string* str2;
str2 = new std::string("This is string two");

Just like C strings in character arrays, you can change the string data that a string object holds. This is unlike Java, where Java Strings are essentially immutable (you always create a new result string object when doing Java string operations).

There are almost 20 different constructors for std::string, so there are lots of other choices. The only other one we will mention is

std::string str1(25,'a');

which creates a string object whose initial string is 25 ‘a’ characters.

Operators

In C++, built-in operators can be overloaded – given a specific meaning for a specific class. You should ALMOST ALWAYS avoid doing this with your own classes. The std::string class has the assignment operator ‘=’ defined for itself. You can write

str1 = "This is a new string";

and C++ will make that string the new string data for the existing string object str1. There are other ways you can use the ‘=’ operator too. One other common one is

str1 = str2;

In Java this would make both variables reference the same object! But in C++ this copies the string data of str2 into the string data of str1. They are still seperate and unique objects!

C++ strings have the ‘+’ operator defined to do string concatenation; note that the left-hand side string object is modified, having the right-hand side concatenated to it.

All of the relational operators (<,>,<=,>=,==,!=)are defined for C++ strings. You can use them to compare strings.

C++ strings also have the ‘<<’ and ‘>>’ operators defined, for use with input and output streams.

Useful Class Member Methods

In the table below, “string” refers to the string data, not the string object itself. These are not all of the available methods. The parameters are not defined exactly, since there is often many overloaded versions. You can get the basic meaning.

Method Meaning
size() or length() returns number of characters in string
empty() returns true if string is empty
insert(index, string/char) insert character(s) at index
replace(index, count, string/char) replace a portion of the string
find(string/char) find first occurrence of substring (or char)
rfind(string/char) find last occurrence of substring (or char)
contains(string/char) true if string contains substring (or char)
compare(string) returns int, 0 if strings are equal, nonzero otherwise
starts_with(string/char) returns true if starts with string/char
ends_with(string/char) returns true if ends with string/char
substr(index, count) returns new string object that is a subset of this one

Useful C++ Library String Functions

The above section discussed std::string class methods. The std name is a namespace that also contains plain functions that involve strings. Some useful ones are listed below.

Library Function Meaning
int getline(stream, string, endChar) Gets a string of input from stream, stopping at endChar; returns 0 when no more available
operator << Performs output of the string on a stream
operator >> Performs input of a string on a stream
stoi(string), stol(string) converts digits string to integer, long
stoul(string) Converts digits string to unsigned long
stof(string), stod(string) Converts digits+ string to float, double
to_string(value) Creates string representing the numeric value

There are long long and long double versions of the numeric conversions, too.

Other Resources

CPPReference.com::strings is my go-to site.