How to get first three characters of std::string in C++

To get the first three characters of myString use myString.substr() like this:

example.cpp
std::string firstTwo = myString.substr(0, 3);

The first argument ob substr() is the offset (i.e. how many characters to skip) which is 0 in this case since we don’t want to skip any characters.

The second argument of substr() is the number of characters to get. Since we want to get three characters, this is 3.


Check out similar posts by category: C/C++