去除空格

it2022-05-05  112

1 1. String.trim() 2 3 trim()是去掉首尾空格 4 5 6 7 2.str.replace(" ", ""); 去掉所有空格,包括首尾、中间 8 9 String str = " hell o "; 10 String str2 = str.replaceAll(" ", ""); 11 System.out.println(str2); 12 13 14 15 3.或者replaceAll(" +",""); 去掉所有空格 16 17 18 19 4.str = .replaceAll("\\s*", ""); 20 21 可以替换大部分空白字符, 不限于空格 22 \s 可以匹配空格、制表符、换页符等空白字符的其中任意一个 23 24 25 26 5.或者下面的代码也可以去掉所有空格,包括首尾、中间 27 28 public String remove(String resource,char ch) 29 { 30 StringBuffer buffer=new StringBuffer(); 31 int position=0; 32 char currentChar; 33 34 while(position 35 { 36 currentChar=resource.charAt(position++); 37 if(currentChar!=ch) buffer.append(currentChar); } return buffer.toString(); 38 }

 

转载于:https://www.cnblogs.com/wangying222/p/5924090.html

相关资源:自动去除空格搜索

最新回复(0)