Replacement of spaces in strings (Java version)

Source: Internet
Author: User

Replacement of spaces in strings (Java version)

Solution 1: Time complexity is O (n ^ 2)

Solution 2: The time complexity is O (n)


Code implementation: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4ncjxwcmugy2xhc3m9 "brush: java;"> Package string; public class SpaceStringReplace2 {// len is the total capacity of the array size 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 ++ ;} // convert the space to % 20 characters in length int strNewLen = strOldLen + blackString * 2; if (strNewLen> len) {new ArrayIndexOutOfBoundsException ();} int indexOfOld = strOldLen; // point to '\ 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 );}} Solution 3: JDK built-in string Cutting

Code implementation:

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. }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.