示例 1:
输入:s = "We are happy." 输出:"We%20are%20happy."
我们运用JS代码,可以让字符串每个字母遍历,遇到字母就不做处理,遇到空格就把它变成%20
两种解决方法
/**
* @param {string} s
* @return {string}
*/
var replaceSpace = function(s) {
let p = s
let re = ''
for(let i = 0;i<s.length;i++){
if(p[i]===' '){
re = re + "%20"
continue
}
re = re+p[i]
}
return re ;
};
方法2;直接运用JS中的replaceAll语法进行替换(这不是这道题的本意)
<script>
var replaceSpace = function(s) {
return s.replaceAll(" ","%20")
};
</script>