zoukankan      html  css  js  c++  java
  • 删除字符串中的多余空格

    //For example:       I’m  very happy   to  introduce m    yself  -->          to you  here
    // To  I’m very happy to introduce m yself --> to you here        


    //Codes:
      String str = "      I’m  very happy   to  introduce m    yself  -->          to you  here       ";
        char[] charArray = str.toCharArray();
        int index = 0;
        int movePoint = 0;
        for(; index<charArray.length; index++){
            if(charArray[index] == ' '){
                while(movePoint<charArray.length && charArray[movePoint] == ' '){
                    movePoint++;
                }
                while(movePoint<charArray.length && charArray[movePoint] != ' '){
                    index++;
                    charArray[index] = charArray[movePoint];
                    movePoint++;
                }   
                if(movePoint >=charArray.length ){
                    break;
                }
            } else{
                if(index != 0){
                    charArray[index] = ' ';
                    index = index-1;
                }
            }
        }
        String result = String.valueOf(charArray, 0, index+1);
        System.out.println(result);
    
    
    
    
    
    清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己 -- 共勉
  • 相关阅读:
    哈夫曼树
    MongoDB
    Node.js 搭建Web
    Node.js
    HDFS编程
    scp
    MapRecude
    级数
    (转)MySQL百万级数据库优化
    ssh
  • 原文地址:https://www.cnblogs.com/hello-yz/p/3843910.html
Copyright © 2011-2022 走看看