0
点赞
收藏
分享

微信扫一扫

去除字符串两边 或 所有空格

生活记录馆 2022-01-30 阅读 178

去除字符串两边的空格

String 类的 trim() 方法主要用于:去除字符串两边的空格

public class Main {

    public static void main(String[] args) {
        String str = " We Are Happy! ";
        System.out.println(str.trim());
    }
}

去除字符串所有空格

使用 String 类的 replace() 方法可将字符串中所有空格符移除

public class Main {

    public static void main(String[] args) {
        String str = " We Are Happy! ";
        System.out.println(str.replace(" ", ""));
    }
}
举报

相关推荐

0 条评论