请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
import java.util.Scanner; import java.util.regex.*; public class Solution { public String replaceSpace(StringBuffer str) { Matcher matcher = Pattern.compile(“\s”).matcher(str); return matcher.replaceAll(“%20”); } public static void main(String[] args) { // TODO Auto-generated method stub Solution func = new Solution(); Scanner scanner = new Scanner(System.in); String str =scanner .nextLine(); StringBuffer str1 = new StringBuffer(str); System.out.println(func.replaceSpace(str1)); scanner.close(); } }