I believe you're using org.springframework.util.StringUtils, which has no isEmpty().
There is, however, an isEmpty() in org.apache.commons.lang.StringUtils.
Ensure that you're using the correct StringUtils.
I believe you're using org.springframework.util.StringUtils, which has no isEmpty().
There is, however, an isEmpty() in org.apache.commons.lang.StringUtils.
Ensure that you're using the correct StringUtils.
However Question was Answered as expected long back, but I think one important point is a must to mention here (as now a days most of projects are Maven based). So don't forget to add maven dependency for 'apache common lang' in your projects POM.xml before importing StringUtils for same. Else there wont be any import option.
Copy<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
[Spring 5.3.0] StringUtils#isEmpty has bean deprecated
Deprecate `StringUtils.isEmpty(Object)` and replace remaining usage (e.g. with `ObjectUtils.isEmpty`)
Replace deprecated StringUtils.isEmpty method with StringUtils.hasLength
Deprecated StringUtil
Useful method from Apache Commons:
org.apache.commons.lang.StringUtils.isBlank(String str)
https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isBlank(java.lang.String)
To detect if a string is null or empty, you can use the following without including any external dependencies on your project and still keeping your code simple/clean:
if(myString==null || myString.isEmpty()){
//do something
}
or if blank spaces need to be detected as well:
if(myString==null || myString.trim().isEmpty()){
//do something
}
you could easily wrap these into utility methods to be more concise since these are very common checks to make:
public final class StringUtils{
private StringUtils() { }
public static bool isNullOrEmpty(string s){
if(s==null || s.isEmpty()){
return true;
}
return false;
}
public static bool isNullOrWhiteSpace(string s){
if(s==null || s.trim().isEmpty()){
return true;
}
return false;
}
}
and then call these methods via:
if(StringUtils.isNullOrEmpty(myString)){...}
and
if(StringUtils.isNullOrWhiteSpace(myString)){...}