使用stringbuffer自带函数解 字符串替换 int index = 0; while (index < str.length()) { index = str.indexOf(" ", index); if (index != -1) { str.replace(index, ++index, "%20"); } else { break; } } return str.toString();