std::basic_string

cppreference.com

Example

main: () = {
    using std::literals::_;
 
    // Creating a string from const char*
    str1: std::string = "hello";
 
    // Creating a string using string literal
    str2:= "world"s;
 
    // Concatenating strings
    str3: std::string = str1 + " " + str2;
 
    // Print out the result
    std::cout << str3 << '\n';
 
    pos:= str3.find(" ");
    str1 = str3.substr(pos + 1u); // the part after the space
    str2 = str3.substr(0u, pos);  // the part till the space
 
    std::cout << "(str1)$ (str2)$\n";
 
    // Accessing an element using subscript operator[]
    std::cout << str1[0] << '\n';
    str1[0] = 'W';
    std::cout << str1 << '\n';
}

Output

hello world
world hello
w
World

Back to top

cpp2reference.com licensed under CC-BY-SA and GFDL.