Java里如何判断一个String是空字符串或空格组成的字符串

 

        要判读String是否为空字符串,比较简单,只要判断该String的length是否为0就可以,或者直接用方法isEmpty()来判断。

        但很多时候我们也会把由一些不可见的字符组成的String也当成是空字符串(e.g, space, tab, etc),这时候就不能单用length或isEmpty()来判断了,因为technically上来说,这个String是非空的。这时候可以用String的方法trim(),去掉前导空白和后导空白,再判断是否为空。

 1public class TestEmpty
 2{
 3    public static void main(String[] args){
 4        String a = "       ";
 5        
 6        // if (a.isEmpty())
 7        if (a.trim().isEmpty())
 8        {
 9            System.out.println("It is empty");
10        }
11        else 
12        {
13            System.out.println("It is not empty");
14        }
15    }
16}
17

原文地址:https://www.cnblogs.com/kakaisgood/p/8880336.html