题目:请实现一个函数,把字符串中的每个空格替换为"%20",例如“We are happy.”,则输出:"We%20are%20happy."
public class Solution { public String replaceSpace(StringBuffer str) { StringBuffer res = new StringBuffer(); for(int i = 0; i < str.length(); i ++){ if(str.charAt(i) == ' '){ //判断是否为空格,若为空格给新StringBuffer直接添加“%20” res.append("%20"); }else { //若不为空格,则给res直接添加原字符 res.append(str.charAt(i)); } } return res.toString(); } }