在js中删除字符串中空格我们常用的函数有String.trim是去掉首尾空格 ,但是要去除包括有掉所有空格,包括首尾、中间 我们可以使用正则str.replace来替换空格了
1. String.trim()
trim()是去掉首尾空格
2.str.replace(" ", ""); 去掉所有空格,包括首尾、中间
代码如下 | 复制代码 | ||||||||||||
String str = " hell o "; String str2 = str.replaceAll(" ", ""); System.out.println(str2); 3.或者replaceAll(" ",""); 去掉所有空格
|