I have a input dialog that asks for XML element name, and I want to check it to see if it has any spaces.
can I do something like name.matches()?
In order to check if a String has only unicode digits or space in Java, we use the isDigit() method and the charAt() method with decision making statements. The isDigit(int codePoint) method determines whether the specific character (Unicode codePoint) is a digit. It returns a boolean value, either true or false.
Python String isspace() The isspace() method returns True if there are only whitespace characters in the string. If not, it return False. Characters that are used for spacing are called whitespace characters. For example: tabs, spaces, newline, etc.
Why use a regex?
name.contains(" ")
That should work just as well, and be faster.
If you will use Regex, it already has a predefined character class "\S" for any non-whitespace character.
!str.matches("\\S+")
tells you if this is a string of at least one character where all characters are non-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