Possible Duplicate:
If string only contains spaces?
I do not want to change a string nor do I want to check if it contains white space. I want to check if the entire string is ONLY white space. What the best way to do that?
Python String isspace() The isspace() method returns True if there are only whitespace characters in the string. If not, it return False.
Python String isspace() is a built-in method used for string handling. The isspace() method returns “True” if all characters in the string are whitespace characters, Otherwise, It returns “False”. This function is used to check if the argument contains all whitespace characters such as: ' ' – Space.
The RegExp \s Metacharacter in JavaScript is used to find the whitespace characters. The whitespace character can be a space/tab/new line/vertical character. It is same as [ \t\n\r].
This will be the fastest way:
$str = ' '; if (ctype_space($str)) { }
Returns false on empty string because empty is not white-space. If you need to include an empty string, you can add || $str == ''
This will still result in faster execution than regex or trim.
ctype_space
since trim returns a string with whitespace removed, use that to check
if (trim($str) == '') { //string is only whitespace }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With