![]() ![]() The example above also demonstrates a method that Groovy's GDK extension of String provides: isAllWhitespace().Īn advantage of the above instance-level approach is that we can ask the String itself if it is empty of white space. With Groovy clients and the safe navigation operator, this is not as much of a deal-breaker as it might be in Java. The reason the methods discussed previously checked for null is that they were static methods working on a provided instance (and not themselves). I don't have a null check because an instance method, by its nature, will only work on a non-null instance. The above Groovy snippet will inject an "isNotBlank()" method on String instances when invocations of a method of that name are not resolved ( missing method). isAllWhitespace() method that returns true} for all white space Do NOT need to check for null because this method could not have been Using thodMissing to 'Add' isNotBlank() Method to String .methodMissing= In this case, I'm borrowing from Apache Commons Lang's name for the method and using isNotBlank() for the injected method name. The Groovy code in the next code listing shows how an instance method could be added to String to check for these conditions. It is difficult for me to finish a post without talking about Groovy, so now I transition to the ability to use Groovy's dynamic support to "add" methods to Java's String class to support this check. However, it would be nice to have this support as a standard part of the Java String. They are easy to employ and the Apache Commons Lang approach is particularly concise. The above approaches and variations on the above approaches can be used in Java to determine if a String is not null, not empty, and not white space only. Its Javadoc documentation says as much: 'Checks if a String is not empty (""), not null and not whitespace only.' With Apache Commons Lang, one simple call does it all! The StringUtils.isNotBlank(String) static method does exactly what we wanted in this particular case: check a String to ensure it is not null, not empty, and not white space only. ![]() Public static boolean isNotNullNotEmptyNotWhiteSpaceOnlyB圜ommons( * space only using Apache Commons Lang classes. ![]() The example using Apache Commons Lang is simplest and most concise approach as depicted in the next code listing.Īpache Commons Lang Approach // import .StringUtils However, Guava provides the convenient Strings.isNullOrEmpty(String) static method for determining if a given String is null or empty. The Guava approach above uses the "standard Java" approach for determining that the String is not white space only. Return !Strings.isNullOrEmpty(string) & !im().isEmpty() Public static boolean isNotNullNotEmptyNotWhiteSpaceOnlyByGuava(final String string) The Google Guava library can also be used effectively for determining a non-null, not empty String with at least one non-whitespace character. Also, the code could be made more concise by not having the individual check for emptiness and simply doing that via the trim().isEmpty() call. ![]() There are other approaches as well including using regular expressions. In the above example, one might compare length of Strings rather than call the isEmpty() method. Return string != null & !string.isEmpty() & !im().isEmpty() Public static boolean isNotNullNotEmptyNotWhiteSpaceOnlyByJava( * has at least one character that is not considered white space. * true} if provided String is not null, is not empty, and * string String to be checked for not null, not empty, and not white * space only using standard Java classes. * Demonstrate checking for String that is not null, not empty, and not white These potentially very useful (and commonly used) methods are not part of Java's standard JDK String, but in this post I look at how Apache Commons Lang and Guava provide methods similar to these or from which methods similar to these can be easily written.Ī typical "standard Java" approach for detecting whether a String is null, is empty, or consists solely of white space is as shown in the next code listing. NET Framework 4 introduces a new method on its String class called IsNullOrWhiteSpace that checks whether a provided String is null, empty, or consists only of "white space." This handy method is in addition to the method IsNullOrEmpty that has been available since. ![]()
0 Comments
Leave a Reply. |