请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
代码:
public static String function(StringBuffer string){
String str = string.toString();
String str1 = str.replace(" ","%20");
return str1;
}
测试方法及主函数:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuffer stringBuffer = new StringBuffer(scanner.nextLine());
System.out.println(function(stringBuffer));
}