Java处理空格【2种情况】

Java中处理空格:

包括  a:去掉首尾空格  b:去掉所有空格,包括首尾、中间

代码如下:

  System.out.println("//===========");
        String s = " a b c ";
        if (s != null) {
            // s.trim();是去掉首尾空格
            s = s.trim();
        }
        System.out.println(s);
        System.out.println("//===========");
        String s2 = " a b c ";
        if (s2 != null) {
            // s.replace(" ", ""); 是去掉所有空格,包括首尾、中间
            s2 = s2.replaceAll(" ", "");
        }
        System.out.println(s2);

 效果图:

原文地址:https://www.cnblogs.com/wangzn/p/7209500.html