Skip to content
  • std::string · C++

std::string

LearnViaNotes @learnvianotes· 3 min read

std::string is a growable, owning sequence of char with value semantics — copying a string copies its characters. Forget char* for text.

Create, combine, access

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

int main() {
    std::string a = "Hello";
    std::string b(3, '!');              // "!!!"
    std::string c = a + ", world" + b;  // concatenation
    c += '?';
    std::cout << c << ' ' << c.size() << '\n';
    std::cout << c[0] << c.front() << c.back() << ' ' << c.at(4) << '\n';
    std::cout << std::boolalpha << c.empty() << ' ' << (a == "Hello") << ' ' << (a < "Help") << '\n';
    for (char& ch : a) ch = static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));
    std::cout << a << '\n';
}
Output
Hello, world!!!? 16
HH? o
false true true
HELLO

Search & modify

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

int main() {
    std::string s = "the quick brown fox";
    std::cout << s.find("quick") << ' ' << s.find('o') << ' ' << s.rfind('o') << '\n';
    if (s.find("cat") == std::string::npos) std::cout << "no cat\n";

    std::cout << s.substr(4, 5) << '|' << s.substr(16) << '\n';   // substr(pos, count)
    s.replace(4, 5, "slow");                                      // replace(pos, count, text)
    s.insert(0, ">> ");
    s.erase(s.size() - 4);                                        // erase from pos to end
    std::cout << s << '\n';
    std::cout << s.starts_with(">>") << s.ends_with("brown") << '\n';  // C++20
}
Output
4 12 17
no cat
quick|fox
>> the slow brown
11
OperationCallNotes
Lengths.size() / s.length()O(1)
Finds.find(x, pos), rfind, find_first_ofReturns index or std::string::npos
Substrings.substr(pos, len)Returns a new string (copy)
Modify+=, push_back, pop_back, insert, erase, replace, clearAmortised O(1) append
Compare==, <, s.compare(t)Lexicographic
Prefix/suffixstarts_with, ends_with (C++20), contains (C++23)
C strings.c_str()Null-terminated const char* for C APIs

Numbers ↔ strings

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

int main() {
    int n = std::stoi("42");
    double d = std::stod("3.5");
    long long big = std::stoll("9000000000");
    std::string s = std::to_string(n + 1) + "/" + std::to_string(big);
    std::cout << n * 2 << ' ' << d * 2 << ' ' << s << '\n';
    std::cout << std::stoi("ff", nullptr, 16) << ' ' << std::stoi("  12abc") << '\n';   // base 16; stops at non-digits
    try { std::cout << std::stoi("abc"); } catch (const std::invalid_argument&) { std::cout << "invalid\n"; }
}
Output
84 7 43/9000000000
255 12
invalid

Splitting and joining

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

std::vector<std::string> split(const std::string& s, char delim) {
    std::vector<std::string> parts;
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) parts.push_back(item);
    return parts;
}

int main() {
    auto parts = split("red,green,,blue", ',');
    std::cout << parts.size() << '\n';
    std::string joined;
    for (std::size_t i = 0; i < parts.size(); ++i) joined += (i ? " | " : "") + parts[i];
    std::cout << joined << '\n';
}
Output
4
red | green |  | blue

What does std::string::npos mean?

Is std::string null-terminated?

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report