题目描述
StringBuffer res;
遍历str,遇到’ ‘(空格),res.append(“%20”),遇到非空格,res.append(‘原字符’);
样例
class Solution {
public String replaceSpaces(StringBuffer str) {
StringBuffer res = new StringBuffer();
for(int i = 0; i < str.length(); i++) {
Character c = str.charAt(i);
if(c.equals(' ')) {
res.append("%20");
}
else{
res.append(c);
}
}
return res.toString();
}
}