How do I check that a Java String is not all whitespaces?
Shortest solution I can think of:
if (string.trim().length() > 0) ...
This only checks for (non) white space. If you want to check for particular character classes, you need to use the mighty match()
with a regexp such as:
if (string.matches(".*\\w.*")) ...
...which checks for at least one (ASCII) alphanumeric character.
Check if a string has only white spaces in it
You could use trimws
, which removes leading/trailing white-space from a character string:
trimws(str) == ""
#[1] TRUE
Check if string has a space
let url = "http://www.example.com/images/pretty pic.png"
let whiteSpace = " "
if let hasWhiteSpace = url.rangeOfString(whiteSpace) {
print ("has whitespace")
} else {
print("no whitespace")
}
Related Topics
How to Move Focus on Next Field When Enter Is Pressed
How to Get Around .Push Is Not a Function in JavaScript
Clear an Input Field With Reactjs
How to Output a Pdf Buffer to Browser Using Nodejs
How to Open a File/Browse Dialog Using JavaScript
How to Delete a Query String Parameter in JavaScript
Trigger Event When Element Becomes Visible With Ngif
Filter Json Object Array on Multiple Values or Arguments JavaScript
Removing Number from Notification Icon
How to Add Multiple Values in Json Object and Get an Updated Json File
Check If Second String Is Rotation of Another String
How to Style HTML Select Option Hover and Selected Option Effects
Changing the Value of Json Object's Key, Changes Other Values Also
Show Virtual Keyboard on Mobile Phones in JavaScript
Dynamically Change Background Color on Scroll