去除空格

 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     }  

 

posted on 2016-09-30 16:08  oooo呼呼  阅读(360)  评论(0编辑  收藏  举报