In JavaScript, strings are immutable, which means the best you can do is to create a new string with the changed content and assign the variable to point to it.
You'll need to define the replaceAt() function yourself:
String.prototype.replaceAt = function(index, replacement) {
return this.substring(0, index) + replacement + this.substring(index + replacement.length);
}
And use it like this:
var hello = "Hello World";
alert(hello.replaceAt(2, "!!")); // He!!o World
Answer from Cem Kalyoncu on Stack OverflowIn JavaScript, strings are immutable, which means the best you can do is to create a new string with the changed content and assign the variable to point to it.
You'll need to define the replaceAt() function yourself:
String.prototype.replaceAt = function(index, replacement) {
return this.substring(0, index) + replacement + this.substring(index + replacement.length);
}
And use it like this:
var hello = "Hello World";
alert(hello.replaceAt(2, "!!")); // He!!o World
There is no replaceAt function in JavaScript. You can use the following code to replace any character in any string at specified position:
function rep() {
var str = 'Hello World';
str = setCharAt(str,4,'a');
alert(str);
}
function setCharAt(str,index,chr) {
if(index > str.length-1) return str;
return str.substring(0,index) + chr + str.substring(index+1);
}
<button onclick="rep();">click</button>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
javascript - Replace character at string index - Stack Overflow
Replacing character at a particular index with a string in Javascript , Jquery - Stack Overflow
javascript - Using .replace() at a specific index - Stack Overflow
html - How to replace characters by index in a JavaScript string? - Stack Overflow
Strings are immutable in JavaScript. You have to create a new string instead:
str = str.substring(0, i) + ' ' + str.substring(i + 1);
If you're doing that a lot, you might convert the string to an array of characters, do the replacements, and then convert the array back into a string. Here's an ES2015+ example:
function replaceChar(str) {
return [...str].map(ch => ch === "-" ? " " : ch).join("");
}
console.log(replaceChar("testing-1-2-3"));
Your entire loop can be replaced by the replace method:
str = str.replace(/-/g, " ");
Strings are immutable in Javascript - you can't change individual characters. If you want to do something like that, you'll have to explicitly convert the string to an array first, perform your changes, and then join the array back into a string again:
function replaceChar (str) {
str = [...str];
let i
for (i = 0; i < str.length; i++) {
if (str[i] == '-') {
str[i] = ' '
}
}
return str.join('');
}
console.log(replaceChar('foo-bar-baz'));
Strings are immutable in Javascript - you can't modify them "in place".
You'll need to cut the original string up, and return a new string made out of all of the pieces:
// replace the 'n'th character of 's' with 't'
function replaceAt(s, n, t) {
return s.substring(0, n) + t + s.substring(n + 1);
}
NB: I didn't add this to String.prototype because on some browsers performance is very bad if you add functions to the prototype of built-in types.
Or you could do it this way, using array functions.
var a='I am a man'.split('');
a.splice.apply(a,[7,1].concat('wom'.split('')));
console.log(a.join(''));//<-- I am a woman
function ReplaceAt(input, search, replace, start, end) {
return input.slice(0, start)
+ input.slice(start, end).replace(search, replace)
+ input.slice(end);
}
jsfiddle here
PS. modify the code to add empty checks, boundary checks etc.
From the "related questions" bar, this old answer seems to be applicable to your case. Replacing a single character (in my referenced question) is not very different from replacing a string.
How do I replace a character at a particular index in JavaScript?
String.prototype.replaceAt=function(index, character) {
return this.substr(0, index) + character + this.substr(index+character.length);
}
str.replaceAt(1,"_");
str.replaceAt(2,"_");
Taken from: How do I replace a character at a particular index in JavaScript?
str = str.replace( /^(.)../, '$1__' );
The . matches any character except a newline.
The ^ represents the start of the string.
The () captures the character matched by the first . so it can be referenced in the replacement string by $1.
Anything that matches the regular expression is replaced by the replacement string '$1__', so the first three characters at the start of the string are matched and replaced with whatever was matched by the first . plus __.
The replaceAt() method doesn't seem right for empty string.
Try
String.prototype.replaceAt = function(index, c) {
return this.substr(0, index) + c + this.substr(index + (c.length == 0 ? 1 : c.length));
}
Your replaceAt does not work as you expect when the second argument is a zero-length string:
"( tree)".replaceAt(1,'')//returns "( tree)"
Remember, you're replacing the same number of characters as the string in your second argument. When that string has length zero, you replace zero characters.
Since the string isn't actually altered, character 1 is always ' ', hence your infinite loop.
Note that
"( tree)".substr(0,1) //returns "("
and
"( tree)".substr(1,6) //returns " tree)"