从 Java 中的字符串中删除所有空格
要从字符串中删除所有空格,请使用 replaceAll() 方法,并用空替换所有空格。
假设以下为我们的字符串。
String str = "This is it!";
现在,让我们替换空格,最终删除它们。
String res = str.replaceAll("\s+","");例
public class Demo {
public static void main(String []args) {
String str = "This is it!";
System.out.println("String: "+str);
String res = str.replaceAll("\s+","");
System.out.println("String after deleting whitespace: "+res);
}
}输出
String: This is it! String after deleting whitespace: Thisisit!
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP