简体   繁体   English

如何在不使用 javascript 中的替换函数的情况下替换字符串?

[英]How do I replace a string without using the replace function in javascript?

I'm trying to create a javascript program where I replace all spaces in a string with %20 without using the replace function.我正在尝试创建一个 javascript 程序,在其中我用 %20 替换字符串中的所有空格,而不使用替换函数。 I'm getting an error when I try to run my program.当我尝试运行我的程序时出现错误。 Not sure what's wrong.不知道出了什么问题。

 let urlEncode = function(text) { text = text.trim(); let newstring; for (let i = 0; i < text.length; i++) { if (text[i] == " ") { newstring[i] = "%20"; } else { newstring[i] = text[i]; } } return newstring; }; console.log(urlEncode("blue is greener than purple for sure"));

Strings are immutable - you can't assign to their indicies.字符串是不可变的——你不能分配给它们的索引。 Rather, initialize newstring to the empty string, use += to concatenate the existing string with the new character(s):相反,将newstring初始化为空字符串,使用+=将现有字符串与新字符连接起来:

 let urlEncode = function(text) { text = text.trim(); let newstring = ''; for (const char of text) { newstring += char === ' '? '%20' : char; } return newstring; }; console.log(urlEncode("blue is greener than purple for sure"));

Or, don't reinvent the wheel, and use encodeURIComponent :或者,不要重新发明轮子,而使用encodeURIComponent

 console.log(encodeURIComponent("blue is greener than purple for sure"));

You can use combination of split and join to achieve in simplified way.您可以使用splitjoin组合以简化的方式实现。

 let urlEncode = function(text) { return text .trim() .split(" ") .join("%20"); }; console.log(urlEncode("blue is greener than purple for sure"));

Your Error Is first you must set newstring in first let only create your variable but not assign any value to it then newstring[0] is undefined then you get error i use += for add char to string and it is better你的错误是必须首先设置newstring在第一let只有创建变量,但没有任何值分配给它,然后newstring[0]undefined ,那么你得到的错误,我使用+=为附加字符字符串,这是更好
you can change your Code Like this你可以像这样改变你的代码

 let urlEncode = function(text) { text = text.trim(); let newstring=""; for (let i = 0; i < text.length; i++) { if (text[i] == " ") { newstring+= "%20"; } else { newstring+= text[i]; } } return newstring; }; console.log(urlEncode("blue is greener than purple for sure"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 string.replace() Javascript / Node.js 替换字符串的最后一部分 - How do I replace last part of string using string.replace() Javascript / Node.js 如何用JavaScript替换HTML字符串 - How do I replace a HTML string with JavaScript 如何使用JavaScript字符串替换方法替换“ +”号? - How do I use the JavaScript string replace method to replace the '+' sign? 如何使用javascript替换功能将字符串替换为方括号? - How to replace a string with square brackets using javascript replace function? 如何使用 replace() 替换 Javascript 中的星号? - How do I replace an asterisk in Javascript using replace()? 如何在JavaScript中进行字符串替换 - How to do String replace in javascript 在javascript中,如何在不覆盖以前的粗体字符串的情况下用粗体文本替换多个字符串? - In javascript, how do I replace multiple strings with bold text without overriding the previous bold string? 如何在JavaScript中进行字符串替换以将“9.61”转换为“9:61”? - How do I do string replace in JavaScript to convert ‘9.61’ to ‘9:61’? 如何在JavaScript中替换“-”? - How do i replace “-” in javascript? 如何在使用正则表达式对象的JavaScript字符串替换函数中对$ 2变量进行运算? - How do i operate on a $2 variable in a javascript string replace function which uses regex object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM