Change character in string javascript
There are lot of answers here, and all of them are based on two methods: Show
Personally, I would use these two methods in different cases. Let me explain. @FabioPhms: Your method was the one I initially used and I was afraid that it is bad on string with lots of characters. However, question is what's a lot of characters? I tested it on 10 "lorem ipsum" paragraphs and it took a few milliseconds. Then I tested it on 10 times larger string - there was really no big difference. Hm. @vsync, @Cory Mawhorter: Your comments are unambiguous; however, again, what is a large string? I agree that for 32...100kb performance should better and one should use substring-variant for this one operation of character replacement. But what will happen if I have to make quite a few replacements? I needed to perform my own tests to prove what is faster in that case. Let's say we have an algorithm that will manipulate a relatively short string that consists of 1000 characters. We expect that in average each character in that string will be replaced ~100 times. So, the code to test something like this is:
I created a fiddle for this, and it's here. There are two tests, TEST1 (substring) and TEST2 (array conversion). Results:
It seems that array conversion beats substring by 2 orders of magnitude! So - what the hell happened here??? What actually happens is that all operations in TEST2 are done on array itself, using assignment expression like So, it's all about choosing the right tool for the job. Again. ExamplesReplace Microsoft: let text = "Visit Microsoft!"; Try it Yourself » A global replacement: let text = "Mr Blue has a blue house and a blue car"; Try it Yourself » More examples below. Definition and UsageThe The The NoteIf you replace a value, only the first instance will be replaced. To replace all instances, use a regular expression with the g modifier set. Read more about regular expressions in our:
Syntaxstring.replace(searchValue, newValue) Parameters
Return Value
More ExamplesA global, case-insensitive replacement: let text = "Mr Blue has a blue house and a blue car"; Try it Yourself » A function to return the replacement text: let text = "Mr Blue has a blue house and a blue car"; Try it Yourself » Browser Support
ES1 (JavaScript 1997) is fully supported in all browsers:
Topic: JavaScript / jQueryPrev|Next Answer: Use the JavaScript replace() methodYou can use the JavaScript
Related FAQHere are some more FAQ related to this topic:
Can I change a character in a string JavaScript?Javascript strings are immutable, they cannot be modified "in place" so you cannot modify a single character.
How do I replace a character in a string?The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.
What is replace () in JavaScript?The replace() method searches a string for a value or a regular expression. The replace() method returns a new string with the value(s) replaced. The replace() method does not change the original string.
How do you change a string in JavaScript?To replace text in a JavaScript string the replace() function is used. The replace() function takes two arguments, the substring to be replaced and the new string that will take its place. Regex(p) can also be used to replace text in a string.
|