1std::string str = "hello world";
2char *str = "hello world";
3char str[] = "hello world";
4char str[11] = "hello world";
1// Include the string library
2#include <string>
3
4// Create a string variable
5string greeting = "Hello";
1#include <string>
2#include <iostream>
3#include <type_traits>
4#include <cstring>
5
6int main() {
7 std::string str = "Hello, there";
8 std::cout << std::boolalpha
9 << str.capacity() << ", " << str.size() << ", " << std::strlen(str.data()) // 12, 12, 12
10 << '\n' << std::is_same_v<std::string, std::basic_string<char>> // true
11 << '\n' << str.front() + str.substr(1, 10) + str.back() // Hello there
12 << '\n' << str[0] // H
13 << '\n';
14
15 str += "!";
16 std::cout << str << '\n'; // Hello, there!
17 str.erase(4, 4); // Hellhere!
18 str.pop_back(); // Hellhere
19 str.insert(4, " "); // Hell here
20 std::cout << str << '\n'; // Hell here
21
22}
1// string::operator[]
2#include <iostream>
3#include <string>
4
5int main ()
6{
7 std::string str ("Test string");
8 for (int i=0; i<str.length(); ++i)
9 {
10 std::cout << str[i];
11 }
12 return 0;
13}
1#include <string>
2std::begin | returns an iterator to the beginning of a container
3std::end | returns an iterator to the end of a container
4std::size | returns the length of string
5std::to_string | converts a number to string
6std::stoi | converts a string to a signed integer
7std::getline | read data from an I/O stream into a string
8std::swap | specializes the std::swap algorithm
9std::empty | checks whether the container is empty