leetcode 链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/
题目
解法
直接使用内置函数:replace()
class Solution {
public String replaceSpace(String s) {
String ans = s.replace(" ","%20");
return ans;
}
}
字符替换
class Solution {
public String replaceSpace(String s) {
if(s==null){
return null;
}
char[] cbuf = s.toCharArray();
StringBuilder ans=new StringBuilder();
for(char c:cbuf){
if(c==' '){
ans.append("%20");
}else{
ans.append(c);
}
}
return ans.toString();
}
}