Java:
class Solution {
public String replaceSpace(String s) {
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++){
char c=s.charAt(i);
if(c==' ') sb.append("%20");
else sb.append(c);
}
return sb.toString();
}
}
s.charAt() 方法用于返回字符串s指定索引处的字符。索引范围为从 0 到 length() - 1。
String s = "www.runoob.com"; char result = s.charAt(8); 输出o
class Solution {
public String replaceSpace(String s) {
return s.replace(" ","%20");
}
}