删除字符串中的多余空格

//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);



清醒时做事,糊涂时读书,大怒时睡觉,独处时思考; 做一个幸福的人,读书,旅行,努力工作,关心身体和心情,成为最好的自己 -- 共勉
原文地址:https://www.cnblogs.com/hello-yz/p/3843910.html