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)
Answer from puczo on Stack OverflowUseful 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)){...}
Question about .isEmpty() and .contains("")
New rule to suggest "string".length() == 0 -> "string".isEmpty()
In Java, should I use (String ) isEmpty() or isBlank() when checking an XML object for an empty value?
New Methods on Java Strings With JDK 11
Videos
SORRY
I JUST REALISED I MADE A MISTAKE I WANTED TO ASK ABOUT EQUALS("") BUT AT THE SAME TIME I WAS FIGHTING WITH ANOTHER EXCERCISE IN WHICH I HAD TO USE CONTAINS() METHOD AND THIS MESSED UP MY THOUGHTS SORRY FOR WASTING YOUR TIME :/
but still i was able to get some usefull info about contains method so it wasn't as much wasted effort but still sorry
Hi i'm doing(or atleast trying to do) mooc.fi java part 1 course and there in some exercises is required to check for empty input.
So there is my question what is difference between .isEmpty() and .Equals("") because i like to use the first one but in suggested solutions always is used ".Equals("") and i'm wondering is there any rule for using .isEmpty(), maybe it's the matter of optimalization etc. or maybe both are as good
I know it can be stupid question but it's better to ask that and get rid of bad habits in writing code as soon as possible :D
In advance thanks for answers
P.S i hope everything is easy to understand unfortunately my english is still far from good :D
I have some config file and it is deployed in many regions. Some regions have certain requirements for example lets say one region needed a username and password (basic auth). The current setup is that each deployment has it's own config file and only the instances where it is needed are these values included in the respective config.xml file.
So I have in my Java code some logic that says
'if this element is in the XML, set the corresponding variable with its value, if not do nothing'
So when I'm trying to see if that value is present or if the tag is empty/blank, which String utility should I use to check?
example code (default option left open as empty string):
if (my_config.getString(USER_ID_ELEMENT, "") != null
& my_config.getString(PASSWORD_ELEMENT, "") != null) {
my_userId = m_config.getString(USER_ID_ELEMENT, "");
my_password = m_config.getString(PASSWORD_ELEMENT, "");Here I'm checking if it is not null, but I don't think it's a very good test since I don't know if an empty XML tag is blank or is empty (i think null isn't even an option)?