从Java中的字符串中删除所有空格
要从字符串中删除所有空格,请使用replaceAll()方法,并将每个空格替换为空。
假设以下是我们的字符串。
String str = "就是这个!";
现在,让我们替换最终将其删除的空白。
String res = str.replaceAll("\\s+","");示例
public class Demo {
public static void main(String []args) {
String str = "就是这个!";
System.out.println("String: "+str);
String res = str.replaceAll("\\s+","");
System.out.println("String after deleting whitespace: "+res);
}
}输出结果
String: 就是这个! String after deleting whitespace: Thisisit!
热门推荐
10 祝孙辈生日祝福语简短
11 恋人跨年祝福语大全简短
12 考大学敬酒简短祝福语
13 开工投产大吉祝福语 简短
14 偶然结婚祝福语朋友简短
15 感谢学长学姐祝福语简短
16 戏精的祝福语简短
17 我要结婚祝福语简短
18 婚前女儿生日祝福语简短