Strings

std::string is C++'s text type. Unlike C's character arrays, it knows its own length, grows automatically, can be copied and compared with ordinary operators, and cleans up its memory by itself. It comes with a large set of member functions for searching and editing text.

Creating and combining

C++
#include <iostream>
#include <string>

int main() {
    std::string first = "Ada";
    std::string last{"Lovelace"};
    std::string full = first + " " + last;     // concatenation
    full += ", mathematician";                 // append
    std::string stars(5, '*');                 // five copies of '*'

    std::cout << full << '\n' << stars << '\n';
    std::cout << "length: " << full.size() << '\n';
}
Output
Ada Lovelace, mathematician
*****
length: 27

Strings are values: assigning one string to another copies the text, and changing the copy leaves the original alone.

Characters and indexing

C++
#include <iostream>
#include <string>

int main() {
    std::string word = "kettle";
    std::cout << word[0] << word.back() << '\n';   // first and last character
    word[0] = 'b';
    std::cout << word << '\n';

    for (char c : word) std::cout << c << ' ';      // range-for over the characters
    std::cout << '\n';

    std::cout << word.at(2) << '\n';                // like [] but checks the index
}
Output
ke
bettle
b e t t l e
t

s[i] doesn't check the index — going past the end is undefined behaviour. s.at(i) checks and throws an exception if it's out of range (lesson 42).

Comparing

Unlike C strings, std::string compares by content with the normal operators, in alphabetical (character-code) order:

C++
#include <iostream>
#include <string>

int main() {
    std::string a = "apple", b = "banana";
    std::cout << std::boolalpha;
    std::cout << (a == "apple") << ' ' << (a < b) << ' ' << (std::string{"Zebra"} < a) << '\n';
}
Output
true true true

Uppercase letters sort before lowercase ones, because their character codes are smaller.

Searching

find returns the position of a match, or the special value std::string::npos when there's none:

C++
#include <iostream>
#include <string>

int main() {
    std::string email = "sam.rivera@example.com";
    auto at = email.find('@');
    if (at != std::string::npos) {
        std::cout << "user:   " << email.substr(0, at) << '\n';
        std::cout << "domain: " << email.substr(at + 1) << '\n';
    }
    std::cout << "last dot at " << email.rfind('.') << '\n';
    std::cout << std::boolalpha << email.starts_with("sam") << ' ' << email.ends_with(".org")
              << ' ' << (email.find("river") != std::string::npos) << '\n';
}
Output
user:   sam.rivera
domain: example.com
last dot at 18
true false true

starts_with and ends_with are C++20; C++23 adds contains.

Editing

C++
#include <iostream>
#include <string>

int main() {
    std::string s = "The quick fox";
    s.insert(10, "brown ");          // insert at position 10
    std::cout << s << '\n';
    s.replace(4, 5, "slow");         // replace 5 characters at position 4
    std::cout << s << '\n';
    s.erase(0, 4);                   // erase the first 4 characters
    std::cout << s << '\n';
    s.push_back('!');
    std::cout << s << '\n';
}
Output
The quick brown fox
The slow brown fox
slow brown fox
slow brown fox!

Upper and lower case

There's no built-in to_upper for strings; transform each character with std::toupper from <cctype>:

C++
#include <cctype>
#include <iostream>
#include <string>

std::string to_upper(std::string s) {
    for (char& c : s) {
        c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
    }
    return s;
}

int main() {
    std::cout << to_upper("Hello, World") << '\n';
}
Output
HELLO, WORLD

The unsigned char cast avoids undefined behaviour for characters outside plain ASCII. char& is a reference, so the loop changes the characters in place (lesson 13).

Splitting a string

An std::istringstream treats a string like an input stream — a simple way to split on whitespace or a delimiter:

C++
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::istringstream words{"red green blue"};
    std::string w;
    while (words >> w) std::cout << '[' << w << "] ";
    std::cout << '\n';

    std::istringstream csv{"2026,10,08"};
    std::string part;
    while (std::getline(csv, part, ',')) std::cout << part << ' ';
    std::cout << '\n';
}
Output
[red] [green] [blue]
2026 10 08

C strings

String literals like "hello" are C-style character arrays, not std::strings. They convert to std::string automatically. When a C library needs a C string, s.c_str() provides one. In C++14 and later, a literal with an s suffix is a real std::string:

C++
#include <iostream>
#include <string>

int main() {
    using namespace std::string_literals;
    auto a = "text";      // const char*
    auto b = "text"s;     // std::string
    std::cout << sizeof(a) << ' ' << b.size() << '\n';
}
Output
8 4

Try it yourself

Check whether a phrase is a palindrome, ignoring spaces and case.

Try it yourself · main.cpp
Edit the code, copy it into main.cpp, then compile and run it with c++ -std=c++20 -Wall main.cpp -o main && ./main. How to set up a C++ compiler
Output
Never odd or even: yes
Hello: no
A man a plan a canal Panama: yes