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

阅读 178

2022-01-30

去除字符串两边的空格

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)

0 0 举报