解题思路:该题解法很简单,遍历字符串,遇到空格,则将空格替换成"%20";
1 import java.util.Scanner; 2 3 public class Solution { 4 public static void main(String[] args) { 5 Scanner scanner = new Scanner(System.in); 6 StringBuffer str = new StringBuffer(scanner.nextLine()); 7 String result = replaceSpace(str); 8 System.out.println(result); 9 } 10 public static String replaceSpace(StringBuffer str) { 11 for(int i=0 ; i<str.length() ; i++) { 12 if(str.charAt(i) == ‘ ‘) { 13 str.replace(i,i+1, "%20"); 14 } 15 } 16 return str.toString(); 17 } 18 }
Java实现:请实现一个函数,将一个字符串中的每个空格替换成"%20"。例如,当字符串为We Are Happy. 则经过替换之后的字符串为We%20Are%20Happy。