C++ Strings: std::string and string_view

In C++ use std::string (in <string>), not the C style char array. It grows by itself and has many helper functions.

Basics

#include <iostream>
#include <string>

int main() {
    std::string first = "Riya";
    std::string last = "Sharma";
    std::string full = first + " " + last;       // join
    full += "!";

    std::cout << full << '\n';
    std::cout << full.size() << ' ' << full.length() << '\n';
    std::cout << full[0] << ' ' << full.front() << ' ' << full.back() << ' ' << full.at(1) << '\n';
    std::cout << full.empty() << '\n';
}
Output

Riya Sharma!
12 12
R R ! i
0

Search and change

#include <iostream>
#include <string>

int main() {
    std::string s = "I like C++. C++ is fast.";

    std::cout << s.find("C++") << '\n';                      // first position
    std::cout << s.rfind("C++") << '\n';                     // last position
    std::cout << (s.find("Java") == std::string::npos) << '\n';   // npos means not found
    std::cout << s.substr(7, 3) << '\n';                    // start, length

    s.replace(7, 3, "Rust");
    std::cout << s << '\n';
    s.insert(0, ">> ");
    s.erase(s.size() - 1);                                   // remove the last character
    std::cout << s << '\n';
}
Output

7
12
1
C++
I like Rust. C++ is fast.
>> I like Rust. C++ is fast

Compare

#include <iostream>
#include <string>

int main() {
    std::string a = "apple", b = "banana";
    std::cout << (a == b) << ' ' << (a < b) << ' ' << a.compare(b) << '\n';
    std::cout << a.starts_with("app") << ' ' << a.ends_with("le") << ' ' << a.contains("ppl") << '\n';   // C++20 and C++23
}
Output

0 1 -1
1 1 1

In C++ == compares the content of two std::string objects (it is not a pointer comparison like with C strings).

Loop and change case

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

int main() {
    std::string s = "Hello World";
    for (char c : s) std::cout << c << '.';
    std::cout << '\n';

    std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
    std::cout << s << '\n';

    std::string rev(s.rbegin(), s.rend());
    std::cout << rev << '\n';
}
Output

H.e.l.l.o. .W.o.r.l.d.
HELLO WORLD
DLROW OLLEH

std::string_view: look without copying

std::string_view (C++17) is a light view of a text. It does not copy and does not own the data. Good for function parameters which only read.

#include <iostream>
#include <string>
#include <string_view>

void greet(std::string_view name) {                 // no copy of the text
    std::cout << "Hello, " << name << "!\n";
}

int main() {
    std::string s = "Riya";
    greet(s);
    greet("Aman");
    greet(std::string_view(s).substr(0, 2));
}
Output

Hello, Riya!
Hello, Aman!
Hello, Ri!

Do not keep a string_view longer than the string it points to: that is a dangling view.

Raw strings and escape characters

#include <iostream>

int main() {
    std::cout << "Line 1\nTab:\there \"quoted\" C:\\temp\n";
    std::cout << R"(Raw: no \n escape, "quotes" are fine)" << '\n';
}
Output

Line 1
Tab: here "quoted" C:\temp
Raw: no \n escape, "quotes" are fine

C style strings

A string literal like "hi" is really an array of char ending with '\0'. Use std::string and use c_str() only when an old C function needs it.

Note: Pass std::string as const std::string& or as std::string_view to avoid copies. Next: C++ if else.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.