package valley.day07;
public class BlankReplace {
public static void main(String[] args) {
String s = "We are happy.";
BlankReplace test = new BlankReplace();
String re = test.replaceSpace(s);
System.out.println(re);
//*********************************
char re1 = test.replaceSpace1(s);
System.out.println(re1);
}
public String replaceSpace(String s) {
//使用StringBuilder去拼接
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
if (" ".equals(String.valueOf(c))) {
sb.append("%20");
} else {
sb.append(s.charAt(i));
}
}
return sb.toString();
}
//To loop every char
public char replaceSpace1(String s) {
for (int i = 0; i < s.length(); ++i) {
char c = s.charAt(i);
System.out.print(c);
System.out.print("|");
}
return ' ';
}
}