使用 Hutool 工具类库可以很方便地去除字符串中的空格。具体的代码如下:

import cn.hutool.core.util.StrUtil;

public class Main {
    public static void main(String[] args) {
        String str = '  Hello World!  ';
        String result = StrUtil.removeAllBlank(str);
        System.out.println(result); // 输出:HelloWorld!
    }
}

上述代码中,首先导入了 cn.hutool.core.util.StrUtil 类,然后通过调用 StrUtil.removeAllBlank 方法去除字符串中的所有空格,返回结果即为去除空格后的字符串。输出结果为 HelloWorld!


原文地址: http://www.cveoy.top/t/topic/pkyB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录