最近整理之前自己学习Java时的一些代码笔记,可能都是一些比较基础的Java知识,在这里只是给需要的人参考一下。
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
package interview;
public class ReplaceSpace {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("wo zai zhe li");
System.out.println(replaceSpace(sb));
}
public static String replaceSpace(StringBuffer str) {
int index = 0;
int spaceLocation = 0;
while(true){
spaceLocation = str.indexOf(" ",index);
if(spaceLocation == -1)
break;
str.replace(spaceLocation, spaceLocation+1, "%20");
index = spaceLocation+1;
}
return str.toString();
}
}