Site Tools


wiki:hpp-string

Table of Contents

<string>

<string> provides std::string (mutable) and std::string_view (read-only, C++17). std::string is a dynamic array of characters with O(1) amortized append, O(1) random access, and utilities like find(), substr(), replace().

Use std::string for owned text; use std::string_view for non-owning references (parameters, returns).

Example

This example demonstrates string length queries, indexing, searching, and replacement operations.

// compile: g++ -std=c++11 -o stringexample stringexample.cpp
// run: ./stringexample
// description: string operations
 
#include <string>
#include <iostream>
 
int main() {
    std::string s = "hello world";
 
    std::cout << "length: " << s.length() << "\n";
    std::cout << "at index 6: " << s[6] << "\n";
 
    size_t pos = s.find("world");
    std::cout << "found at: " << pos << "\n";
 
    s.replace(0, 5, "goodbye");
    std::cout << "after replace: " << s << "\n";
 
    return 0;
}
wiki/hpp-string.md · Last modified: by 127.0.0.1