题目描述
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy
public class Solution { public String replaceSpace(StringBuffer str) { String str1 = str.toString(); char[] charArray = str1.toCharArray(); StringBuffer sBuffer = new StringBuffer(); for(char c : charArray){ if(c == ' '){ sBuffer.append("%20"); } else{ sBuffer.append(c); } } String string = sBuffer.toString(); return string; } }