去除字符串两边的空格
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(" ", ""));
}
}