解法1:遍历
public String replaceSpace (String s) {
// write code here
char[] ch=s.toCharArray()
StringBuffer str=new StringBuffer();
for(char i : ch){
if(String.valueOf(i).equals(" ")){
str.append("%20");
}
else{
str.append(i);
}
}
return str.toString();
}
解法2:String.replaceAll()
public String replaceSpace (String s) {
if (s == null || "".equals(s))
return s;
return s.replaceAll(" ", "%20");
}
解法3:Spring.split()
public String replaceSpace (String s) {
// write code here
StringBuffer str=new StringBuffer();
String[] arr=s.split("");
for(String ss : arr){
if(ss.equals(" ")){
str.append("%20");
}
else{
str.append(ss);
}
}
return str.toString();
}