As the title says, I'd like to see if a substring of a string is at a particular position within the string. Let's say I have the string "IDENTIFIER 10239", and I want to see if "IDENTIFIER" is the first 10 letters within the string. I could do something like !str.substr(0,10).compare("IDENTIFIER"), and this works fine. Could there be a more efficient way of doing it, though? I could do something like str.size() > 9 && str[0] == 'I' && str[1] == 'D", etc, but that seems like a very long way of going about it (could be simplified with a loop, but still...). I could also use str.find("IDENTIFIER"), but there's no option for me to limit the upper bound of the search area, just the lower bound. Does a better way exist?
↧