/** * @param {string} s * @return {string} */ var replaceSpace = function (s) { return s.replace(/\s/g, '%20') }
分割
1 2 3 4 5 6 7
/** * @param {string} s * @return {string} */ var replaceSpace = function (s) { return s.split(' ').join('%20') }
遍历+字符串拼接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * @param {string} s * @return {string} */ var replaceSpace = function (s) { let ans = '' for (let i = 0; i < s.length; i++) { if (s[i] === ' ') { ans += '%20' } else { ans += s[i] } } return ans }