for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ' ') { str.replace(i, i+1, "%20"); } } return str.toString();