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

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

std::string firstTwo = myString.substr(0, 2);

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 two characters, this is 2.