What is the function of the substr() method in C++?
In C++, the substr function is used to extract a substring from a string. This function takes two parameters: the starting position (index) and the length of the substring. It returns a new string that includes the specified length of characters starting from the starting position in the original string.
For example, suppose we have a string s = “Hello, World!” and we can use the substr function to extract a substring from it.
string s = "Hello, World!";
string sub = s.substr(7, 5); // 从索引位置7开始,提取长度为5的子串
In the above example, the value of “sub” will be “World”, because the 5 characters starting from index position 7 are “World”.
Please note that the first parameter of the substr function represents the index of the starting position, not the starting position of the substring. Index starts from 0, so index position 7 is actually the 8th character in the string.