To create a substring from a string, we use the .substr()
member function.
The function returns a substring that starts at a certain position in the main string and is of a certain length.
The signature of the function is:
.substring(starting_position, length).
Example:
#include <iostream> #include <string> int main() //ww w .j av a 2 s .c om { std::string s = "Hello World."; std::string mysubstring = s.substr(6, 5); std::cout << "The substring value is: " << mysubstring; }
In this example, we have the main string that holds the value of "Hello World."
Then we create a substring that only has the "World" value.
The substring starts from the sixth character of the main string, and its length is five characters.