Java String Replace replaceSpace(String str, int actualLength)

Here you can find the source of replaceSpace(String str, int actualLength)

Description

replace Space

License

Apache License

Declaration

public static String replaceSpace(String str, int actualLength) 

Method Source Code


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

Related

  1. replaceIgnoreCase(String line, String oldString, String newString)
  2. replaceIgnoreCase(String source, String strBeReplace, String strReplaced)
  3. replaceNonPrintableAsciiCharacters(String str)
  4. replaceOnce(String template, String placeholder, String replacement)
  5. replaceSign(String sourceStr, char startc, char endc, String replaceStr)
  6. replaceUnsafeNamespaceDelimiters(String input)
  7. replaceWildcards(String wildcardPattern)
  8. searchAndReplace(String source, String search, String replace)
  9. strReplaceOld(String strText, String strFrom, String strTo, boolean bMultiple)