題目:請實作一個函式,將一個字串中的每個空格替換成“%20”,例如,當字串為We Are Happy.則經過替換之后的字串為We%20Are%20Happy,
- 代碼如下
1 public class Demo6 { 2 3 public static void main(String[] args) { 4 String str = "We Are Happy"; 5 String replaceSpace = replaceSpace(new StringBuffer(str)); 6 System.out.println(replaceSpace); 7 } 8 9 public static String replaceSpace(StringBuffer str) { 10 if(str == null){ 11 return null; 12 } 13 //把str轉成String型別 14 String string = str.toString(); 15 String replaceStr = string.replace(" ", "%20"); 16 return replaceStr; 17 } 18 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/62066.html
標籤:其他
上一篇:6-二維陣列中的查找
下一篇:8-斐波那契數列
