请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
public class Solution {
public String replaceSpace(StringBuffer str) {
if(null == str){
return null;
}else{
String string = str.toString();
StringBuffer result = new StringBuffer();
for(int i=0; i<string.length(); i++){
if(string.charAt(i) == ' ')
result.append("%20");
else
result.append(string.charAt(i));
}
return result.toString();
}
}
public static void main(String args[]){
StringBuffer str = new StringBuffer("We Are Happy");
String result = "";
Solution so = new Solution();
result = so.replaceSpace(str);
System.out.println(result);
}
}
主要函数:
String chatAt()
StringBuffer append();