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);
    
    
    
    
    
    清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己 -- 共勉
  • 相关阅读:
    7-36 复数四则运算
    7-35 有理数均值
    7-34 通讯录的录入与显示
    7-33 有理数加法
    7-32 说反话-加强版
    POJ-2524-Ubiquitous Religions
    POJ-2406-Power Strings
    HDU-1072-Nightmares
    POJ-1961-Period(ZOJ-2177)
    POJ-1961-Period(ZOJ-2177)
  • 原文地址:https://www.cnblogs.com/hello-yz/p/3843910.html
Copyright © 2011-2022 走看看