public class test {
public static void main(String[] args) {
String a = "We are happy.";
String bb = replaceSpace(a);
System.out.println(bb);
}
public static String replaceSpace(String s) {
//创建字符串,StringBuffer属于安全性
StringBuffer ss = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
ss.append("%20");
}else {
ss.append(s.charAt(i));
}
}
return ss.toString();
//第二种方法,直接使用字符串replace方法
// return s.replace("", "%20");
}
}