I need a Java regular expression, which checks that the given String is not Empty and not null . However the expression should ingnore if the user has accidentally given whitespace in the beginning of the input, but allow whitespaces later on.
4 Answers
You could do it without using regex.
boolean check(String s) {
return s != null && s.trim().length() > 0;
}
2 Comments
Woot4Moo
@user1324126 that is a terrible idea.
Travis Crum
@Woot4Moo its not a good idea but still necessary for certain things
You can use Guava to check for nullity and emptiness:
Strings.isNullOrEmpty(myString);
And you cannot use regular expressions on a null String.
nullis not a String object.