首页 > 技术文章 > Java处理空格【2种情况】

wangzn 2017-07-20 09:44 原文

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);

 效果图:

 

推荐阅读