Here you can find the source of replaceSpace(String str, int actualLength)
public static String replaceSpace(String str, int actualLength)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.Arrays; public class Main { public static String replaceSpace(String str, int actualLength) { char[] characters = new char[str.length()]; characters = str.toCharArray();//from w w w . ja va 2s. co m ArrayList<Integer> positions = new ArrayList<Integer>(); //mark for (int i = 0; i < actualLength; i++) { if (str.charAt(i) == ' ') { if (positions.isEmpty()) positions.add(i); else { positions.add(i + 2); } } } //sweep and swift the array for (int i = 0; i < positions.size(); i++) { char[] tmp = Arrays.copyOfRange(characters, positions.get(i) + 1, characters.length - 3); characters = Arrays.copyOfRange(characters, 0, positions.get(i) + 3); characters[positions.get(i)] = '%'; characters[positions.get(i) + 1] = '2'; characters[positions.get(i) + 2] = '0'; char[] result = new char[tmp.length + characters.length]; System.arraycopy(characters, 0, result, 0, characters.length); System.arraycopy(tmp, 0, result, characters.length, tmp.length); characters = Arrays.copyOf(result, result.length); } return new String(characters); } }