- 遍历替换
public class Solution { public String replaceSpace(StringBuffer str) { StringBuffer res = new StringBuffer(); for(char c : str.toString().toCharArray()) { if(c == ' ') { res.append("%20"); } else { res.append(c); } } return res.toString(); } }
public class Solution { public String replaceSpace(StringBuffer str) { StringBuffer res = new StringBuffer(); for(char c : str.toString().toCharArray()) { if(c == ' ') { res.append("%20"); } else { res.append(c); } } return res.toString(); } }