1.创建新的字符串
1 public class T004 { 2 public static void main(String[] args){ 3 System.out.println(replaceBlank("we are happy")); 4 } 5 public static String replaceBlank(String s){ 6 String r = "%20"; 7 String result = ""; 8 for(int i=0;i<s.length();i++){ 9 if(s.charAt(i)==‘ ‘){ 10 result +=r; 11 }else{ 12 result += s.charAt(i); 13 } 14 } 15 return result; 16 17 } 18 }
2.在原来的字符串上做替换