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