How can I check to see if a String contains a whitespace character, an empty space or " ". If possible, please provide a Java example.
For example: String = "test word";
String = "test word";
For checking if a string contains whitespace use a Matcher and call its find method.
Matcher
Pattern pattern = Pattern.compile("\s"); Matcher matcher = pattern.matcher(s); boolean found = matcher.find();
If you want to check if it only consists of whitespace then you can use String.matches:
String.matches
boolean isWhitespace = s.matches("^\s*$");
2.1m questions
2.1m answers
60 comments
57.0k users