I am using the substr() function, however it’s not working. My code is below
std::string s1 = ".V/123\n"
".V/233\n";
std::string ss;
if(s1.substr(0,3) == ".V/")
{
ss = s1.substr(3);
std::cout << ss;
} else {
std::cout << "INCORRECT" << std::endl;
}
The output is 123.V/123
Shouldn’t it be:
123
123
Could someone tell me where I am going wrong please?
Your string contains 2 lines. Doing “.V/123\n” “.V/223\n” will do the same as “.V/123\n.V/223\n”. Either split that up into separate variables or an array. What your substr(3) is doing is extracting all the characters in the string from the 4th character to the end, so ‘ss’ is getting set to “123\n.V/223\n”. This is what you’re seeing.
What you want is something like this