zoukankan      html  css  js  c++  java
  • JAVA中去掉空格经典整理

    JAVA中去掉空格经典整理    

    JAVA中去掉空格    
      
        1. String.trim() --------------trim()是去掉首尾空格     
      
        2.str.replace(" ", ""); 去掉所有空格,包括首尾、中间   

      String str = " hell o Wor d";   
          String str2 = str.replaceAll(" ", "");   
          System.out.println(str2);   

        3.或者replaceAll(" +",""); 去掉所有空格

        4.str = .replaceAll("\s*", "");   

            可以替换大部分空白字符, 不限于空格

            s 可以匹配空格、制表符、换页符等空白字符的其中任意一个。

        5.下面的代码也可以去掉所有空格,包括首尾、中间   

     1 public String remove(String resource,char ch)   
     2     {   
     3         StringBuffer buffer=new StringBuffer();   
     4         int position=0;   
     5         char currentChar;   
     6   
     7         while(position<resource.length())   
     8         {   
     9             currentChar=resource.charAt(position++);   
    10             if(currentChar!=ch) buffer.append(currentChar); } return buffer.toString();   
    11     }  
    12 -------------------------------Test-----------------------------------------------------
    13  
    14 package practisedemo;
    15 public class Demo{
    16  
    17  public static String remove(String resource,char ch)   
    18  {   
    19      StringBuffer buffer=new StringBuffer();   
    20      int position=0;   
    21      char currentChar;   
    22 
    23      while(position<resource.length())   
    24      {   
    25          currentChar=resource.charAt(position++);  
    26          //如果当前字符不是要去除的字符,则将当前字符加入到StringBuffer中
    27          if(currentChar!=ch) buffer.append(currentChar); 
    28      } 
    29      return buffer.toString();   
    30  }
    31  public static void main(String args[]) {
    32    String str= "afdsfdf 了   2012   0407 Alamps 老师";
    33    char c=' ';
    34       System.out.println("结果为:"+Demo.remove(str, c));
    35       
    36       
    37  }
    38 }

    实例演示

    有一个字符串开头或结尾含有n个空格(” abcdefgdddd ”),欲去掉前后空格,返回一个新字符串。

     1 int trimSpace(char *bufscr,char *bufdes)
     2 {
     3     int i=0;
     4     char *p=bufscr;//指针变量,为了不修改传入指针
     5     int len=strlen(bufscr);
     6     int j=len-1;
     7     int count;//非空字符长度
     8     int ret=0;
     9     //首先判断传入的指针是否为空
    10     if (bufscr==NULL||bufdes==NULL)
    11     {
    12         ret=-1;
    13         printf("func trimSpace err %d:",ret);
    14         return ret;
    15     }
    16  
    17     while(isspace(p[i])&&p[i]!='')
    18     {
    19         i++;
    20     }
    21  
    22     while(isspace(p[j])&&j>0)
    23     {
    24         j--;
    25     }
    26     count=j-i+1;
    27  
    28     memcpy(bufdes,p+i,count);
    29     //这里一定要注意,字符串数组和字符串的区别就在于字符串数组末尾多''。
    30     bufdes[count]='';
    31     return ret;
    32 }

    打印结果如下所示

            

  • 相关阅读:
    MSBuild、条件编译、预处理命令
    批量数据插入SqlBulkCopy
    WPF 双向绑定
    编程思想之一
    python 提交表单
    python 添加用户
    python 分页
    day9 IO多路复用
    day9 线程、进程和协程深入版
    day8 进程、线程 简介版
  • 原文地址:https://www.cnblogs.com/lwlxqlccc/p/6179296.html
Copyright © 2011-2022 走看看