I know there’s an “isspace” function that checks for spaces, but that would require me to iterate through every character in the string, which can be bad on performance since this would be called a lot. Is there a fast way to check if a std::string contains only spaces?
ex:
function(" ") // returns true
function(" 4 ") // returns false
One solution I’ve thought of is to use regex, then i’ll know that it only contains whitespace if it’s false… but i’m not sure if this would be more efficient than the isspace function.
regex: [\w\W] //checks for any word character(a,b,c..) and non-word character([,],..)
thanks in advance!
Any method would, of necessity, need to look at each character of the string. A loop that calls
isspace()on each character is pretty efficient. Ifisspace()is inlined by the compiler, then this would be darn near optimal.The loop should, of course, abort as soon as a non-space character is seen.