zoukankan      html  css  js  c++  java
  • 去除空格

     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     }  
  • 相关阅读:
    request实现登录
    python之对象
    python基础之迭代与解析
    python基础之函数
    linux expect命令使用入门
    Python socket
    1
    蓝牙
    SQL查询语句
    iOS常用小知识纪录
  • 原文地址:https://www.cnblogs.com/wangying222/p/5924090.html
Copyright © 2011-2022 走看看