1、题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
2、代码实现
public class Solution {
public String replaceSpace(StringBuffer str){
String strTemp = "";
for(int i = 0; i < str.length(); i++){
if(str.charAt(i) == ' '){
strTemp += "%20";
continue;
}
strTemp += str.charAt(i);
} return strTemp;
} public static void main(String[] args) {
Solution solutionBlank = new Solution();
StringBuffer str = new StringBuffer("We Are Happy");
System.out.println(solutionBlank.replaceSpace(str));
}
}