Difference StringUtils.isBlank (str) and StringUtils.isEmpty (str) of

StringUtils.isBlank (str) and the difference between StringUtils.isEmpty (str) or look at their implementation What's the difference

1.StringUtils.isEmpty (CharSequence cs) achieving source

  public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

When the source from the discovery StringUtils.isEmpty (CharSequence cs) is determined as a null or cs cs.length () = 0, but we have to determine special escape characters or newline characters blank, its length is greater than 0 , the judgment is not acceptable isEmpty

2.StringUtils.isBlank (CharSequence cs) achieving source

  •     public static boolean isBlank(CharSequence cs) {
            int strLen;
            if (cs != null && (strLen = cs.length()) != 0) {
                for(int i = 0; i < strLen; ++i) {
                    if (!Character.isWhitespace(cs.charAt(i))) {
                        return false;
                    }
                }
    
                return true;
            } else {
                return true;
            }
        }

    Can be found from, StringUtils.isBlank isEmpty methods than multi Analyzing method Character.isWhitespace i.e. whitespace whitespace are: space, tab key \ n, newline \ t, carriage return \ r, formfeed \ f

        

Test code:

    public  static  void main (String [] args) {
         // isWhitespace () method for determining whether or not the specified character is a blank character, whitespace comprising: space, tab key, line breaks. 
        System.out.println (Character.isWhitespace ( 'C' )); 
        System.out.println (Character.isWhitespace ( '' )); 
        System.out.println (Character.isWhitespace ( '\ n-' )); 
        the System. Out.println (Character.isWhitespace ( '\ T' )); 
        
        String STR = "C" ; 
        String str1 = "" ; 
        String str2 = "" ; 
        String Str3 = "\ n-" ;= "\t";
        System.out.println("is blank:" + StringUtils.isBlank(str3));
        System.out.println("is empty:" + StringUtils.isEmpty(str3));
    }
false
true
true
true
is blank:true
is empty:false
  • 3. Conclusion:

  • To whitespace filtered off with StringUtils.isBlank methods, only if the character is determined whether the length of null or 0 with StringUtils.isEmpty

 

 

 

 

 

 

      

Guess you like

Origin www.cnblogs.com/guanbin-529/p/11729678.html