来源:https://www.nowcoder.com/practice/4060ac7e3e404ad1a894ef3e17650423?tpId=13&tqId=11155&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
题目描述
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuffer result = new StringBuffer();
int length = str.length();
for(int i = 0;i<length;i++){
if(str.charAt(i)!=' '){
result.append(str.charAt(i));
}else{
result.append("%20");
}
}
return new String(result);
}
}