字串中的空格替換問題(Java版)

來源:互聯網
上載者:User

標籤:jdk   替換   ldl   pre   poi   ade   each   log   current   

解決方式一:時間複雜度為O(n^2)

解決方式二:時間複雜度為O(n)


代碼實現:

package string;public class SpaceStringReplace2 {    //len為數組大小的總容量    public static void SpaceReplace(String strOld,int len){        char[] chs =new char[len];        char[] ch = strOld.toCharArray();        for (int i = 0; i < ch.length; i++) {            chs[i] = ch[i];        }        int strOldLen = 0;        int blackString = 0;        if(chs==null || len<=0)        {            new NullPointerException();         }        int i=0;        while(chs[i]!=‘\0‘){            strOldLen++;            if(chs[i]==‘ ‘){                blackString++;            }            i++;        }        //將空格轉換成%20字元的長度        int strNewLen = strOldLen + blackString*2;        if(strNewLen>len){            new ArrayIndexOutOfBoundsException();        }        int indexOfOld=strOldLen;//指向‘\0‘        int indexOfNew=strNewLen;        while(indexOfOld>0 && indexOfNew>indexOfOld){            if(chs[indexOfOld] == ‘ ‘){                chs[indexOfNew--] = ‘0‘;                chs[indexOfNew--] = ‘2‘;                chs[indexOfNew--] = ‘%‘;            }            else{                chs[indexOfNew--] = chs[indexOfOld];            }            --indexOfOld;        }           for (char c : chs) {            if(c==‘\0‘){                break;            }            System.out.print(c);        }        System.out.println();    }    public static void main(String[] args) {        //StringBuilder str = new StringBuilder("We are happy.");        long timelast = System.currentTimeMillis();        String str = "We are happy.";        SpaceReplace(str,100);//We%20are%20happy.        long timeafter = System.currentTimeMillis();        System.out.println(timeafter-timelast);    }}
解決方式三JDK內建的字串分割

代碼實現:

package string;public class SpaceStringReplace {    public static String SpaceReplace(String strOld){        String[] split = strOld.split(" ");        StringBuilder stringBuilder = new StringBuilder();        for (int i = 0; i < split.length-1; i++) {            stringBuilder.append(split[i]).append("%20");        }        stringBuilder.append(split[split.length-1]);        String strNew = stringBuilder.toString();        return strNew;    }    public static void main(String[] args) {        //StringBuilder str = new StringBuilder("We are happy.");        String str = "We are happy.";        System.out.println(SpaceReplace(str));//We%20are%20happy.    }}

字串中的空格替換問題(Java版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.