Tutorial by Examples

Use std::string::substr to split a string. There are two variants of this member function. The first takes a starting position from which the returned substring should begin. The starting position must be valid in the range (0, str.length()]: std::string str = "Hello foo, bar and world!"...
Replace by position To replace a portion of a std::string you can use the method replace from std::string. replace has a lot of useful overloads: //Define string std::string str = "Hello foo, bar and world!"; std::string alternate = "Hello foobar"; //1) str.replace(6, 3,...
You can concatenate std::strings using the overloaded + and += operators. Using the + operator: std::string hello = "Hello"; std::string world = "world"; std::string helloworld = hello + world; // "Helloworld" Using the += operator: std::string hello = "Hel...
There are several ways to extract characters from a std::string and each is subtly different. std::string str("Hello world!"); operator[](n) Returns a reference to the character at index n. std::string::operator[] is not bounds-checked and does not throw an exception. The caller is...
Listed from least expensive to most expensive at run-time: str::strtok is the cheapest standard provided tokenization method, it also allows the delimiter to be modified between tokens, but it incurs 3 difficulties with modern C++: std::strtok cannot be used on multiple strings at the same t...
In order to get const char* access to the data of a std::string you can use the string's c_str() member function. Keep in mind that the pointer is only valid as long as the std::string object is within scope and remains unchanged, that means that only const methods may be called on the object. C++1...
To find a character or another string, you can use std::string::find. It returns the position of the first character of the first match. If no matches were found, the function returns std::string::npos std::string str = "Curiosity killed the cat"; auto it = str.find("cat"); ...
This example requires the headers <algorithm>, <locale>, and <utility>. C++11 To trim a sequence or string means to remove all leading and trailing elements (or characters) matching a certain predicate. We first trim the trailing elements, because it doesn't involve moving any el...
Two std::strings can be compared lexicographically using the operators ==, !=, <, <=, >, and >=: std::string str1 = "Foo"; std::string str2 = "Bar"; assert(!(str1 < str2)); assert(str > str2); assert(!(str1 <= str2)); assert(str1 >= str2); assert...
In C++, sequences of characters are represented by specializing the std::basic_string class with a native character type. The two major collections defined by the standard library are std::string and std::wstring: std::string is built with elements of type char std::wstring is built with e...
C++17 C++17 introduces std::string_view, which is simply a non-owning range of const chars, implementable as either a pair of pointers or a pointer and a length. It is a superior parameter type for functions that requires non-modifiable string data. Before C++17, there were three options for this: ...
C++11 std::string supports iterators, and so you can use a ranged based loop to iterate through each character: std::string str = "Hello World!"; for (auto c : str) std::cout << c; You can use a "traditional" for loop to loop through every character: std::stri...
A std::string containing a number can be converted into an integer type, or a floating point type, using conversion functions. Note that all of these functions stop parsing the input string as soon as they encounter a non-numeric character, so "123abc" will be converted into 123. The s...
Converting between encodings is easy with C++11 and most compilers are able to deal with it in a cross-platform manner through <codecvt> and <locale> headers. #include <iostream> #include <codecvt> #include <locale> #include <string> using namespace std; i...
C++14 In C++14, this is easily done by std::mismatch which returns the first mismatching pair from two ranges: std::string prefix = "foo"; std::string string = "foobar"; bool isPrefix = std::mismatch(prefix.begin(), prefix.end(), string.begin(), string.end()).first == ...
std::ostringstream can be used to convert any streamable type to a string representation, by inserting the object into a std::ostringstream object (with the stream insertion operator <<) and then converting the whole std::ostringstream to a std::string. For int for instance: #include <sst...

Page 1 of 1