Can we rely on String.isEmpty for checking null condition on a String in Java?

前端 未结 6 2369
南旧
南旧 2020-12-24 10:44

I am passing an accountid as input from an XML file as shown, which will be parsed later and will be used in our code:

123456         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 11:09

    I think the even shorter answer that you'll like is: StringUtils.isBlank(acct);

    From the documentation: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#isBlank%28java.lang.String%29

    isBlank
    public static boolean isBlank(String str)
    Checks if a String is whitespace, empty ("") or null.
    
     StringUtils.isBlank(null)      = true
     StringUtils.isBlank("")        = true
     StringUtils.isBlank(" ")       = true
     StringUtils.isBlank("bob")     = false
     StringUtils.isBlank("  bob  ") = false
     
    Parameters:
    str - the String to check, may be null
    Returns:
    true if the String is null, empty or whitespace
    

提交回复
热议问题