zoukankan      html  css  js  c++  java
  • JAVA中去掉空格

    JAVA中去掉空格    
      
    1. String.trim()   
      
    trim()是去掉首尾空格   
      
        
      
    2.str.replace(" ", ""); 去掉所有空格,包括首尾、中间   
      
    String str = " hell o ";   
    String str2 = str.replaceAll(" ", "");   
    System.out.println(str2);   
      
        
      
    3.或者replaceAll(" +",""); 去掉所有空格   
      
        
      
    4.str = .replaceAll("\\s*", "");   
      
    可以替换大部分空白字符, 不限于空格    
    \s 可以匹配空格、制表符、换页符等空白字符的其中任意一个   
      
        
      
    5.或者下面的代码也可以去掉所有空格,包括首尾、中间   
      
    public String remove(String resource,char ch)   
        {   
            StringBuffer buffer=new StringBuffer();   
            int position=0;   
            char currentChar;   
      
            while(position<resource.length())   
            {   
                currentChar=resource.charAt(position++);   
                if(currentChar!=ch) buffer.append(currentChar); } return buffer.toString();   
        }  
    -------------------------------Test-----------------------------------------------------

     
    package practisedemo;
    public class Demo{
     
     public static String remove(String resource,char ch)   
     {   
         StringBuffer buffer=new StringBuffer();   
         int position=0;   
         char currentChar;   

         while(position<resource.length())   
         {   
             currentChar=resource.charAt(position++);  
             //如果当前字符不是要去除的字符,则将当前字符加入到StringBuffer中
             if(currentChar!=ch) buffer.append(currentChar); 
         } 
         return buffer.toString();   
     }
     public static void main(String args[]) {
       String str= "afdsfdf 了   2012   0407 Alamps 老师";
       char c=' ';
          System.out.println("结果为:"+Demo.remove(str, c));
          
          
     }
    }

    转自:https://www.cnblogs.com/alamps/archive/2012/04/27/2473694.html
  • 相关阅读:
    Linux rabbitmq的安装和安装amqp的php插件
    php 安装xdebug扩展
    php 安装pdo_mysql 扩展
    php 安装redis扩展
    给定a、b两个文件,各存放50亿个url,每个url各占用64字节,内存限制是4G,如何找出a、b文件共同的url?
    php 实现多线程
    PHP-Fcgi下PHP的执行时间设置方法
    nginx php 安装
    PHP-fpm启动时 出现 PHP Warning: PHP Startup: Invalid library (maybe not a PHP library) 'fileinfo.so' in Unknown on line 0
    CentOS 6.7 编译PHP7 make时出现错误:undefined reference to `libiconv_close’
  • 原文地址:https://www.cnblogs.com/javalinux/p/15691193.html
Copyright © 2011-2022 走看看