Replace A Character At A Specific Index In JavaScript

To replace a character at a specific index in JavaScript, use any of the following methods –

  1. substring() method
  2. split() and join() methods

Let’s see each of these methods in detail below.

replace character at a specific index in javascript

Replace A Character At A Specific Index In JavaScript Using substring()

The substring() method is used to extract a part of the string, starting from a specified index and going up to another specified index. If only one argument is provided, then it will start from that index till the end of the string.

To replace a character at a specific index, we can first use the substring() method to extract the part of the string up to but not including the index of the character to be replaced.

Then we can use the + operator to concatenate the replacement character at the given index.

Then, we can use the + operator to concatenate the remaining part of the string starting from the next index.

For example,

var str = "JavaScript";

var index = 4;

var res = str.substring(0, index) + "-" + str.substring(index + 1);

console.log(res); // Output: Java-cript

In the above example, we have extracted the part of the string up to but not including the index of 4.

Then we have concatenated the replacement character – at that index.

Lastly, we have concatenated the remaining part of the string starting from 5.

Replace A Character At A Specific Index In JavaScript Using split() And join() Methods

In JavaScript, strings are immutable. This means that once a string is created, it is not possible to modify it.

But there are ways to create a new string with the desired modifications. One way to do this is by using an array of characters.

We can first convert the given string into an array of characters using the split() method.

Then we can use the splice() method to modify the array by replacing the character at the given index with the replacement character.

Finally, we can convert the array back into a string using the join() method and assign it to a variable.

For example,

var str = "JavaScript";

var index = 4;

var res = str.split("");

res.splice(index, 1, "-");

res = res.join("");

console.log(res); // Output: Java-cript

In the above example, we have first converted the string into an array of characters using the split() method.

Then we have replaced the character at index 4 with – using the splice() method.

Lastly, we have converted the array back into a string using the join() method and assigned it to a variable.

Replace Multiple Characters At A Given Index

We can also replace multiple characters at a given index. For example,

var str = "JavaScript";

var index = 4;

var replacement = "type";

var res = str.substring(0, index) + replacement + str.substring(index + replacement.length);

console.log(res); // Output: Javatypept

In the above example, we have replaced the characters at index 4 with type. We have calculated the length of the replacement string and used it to extract the remaining part of the string.

We can also use the split() and join() methods to replace multiple characters at a given index. For example,

var str = "JavaScript";

var index = 4;

var replacement = "type";

var res = str.split("");

res.splice(index, replacement.length, replacement);

res = res.join("");

console.log(res); // Output: Javatypept

In the above example, we have first converted the string into an array of characters using the split() method.

Then we have replaced the characters at index 4 with type using the splice() method.

Lastly, we have converted the array back into a string using the join() method and assigned it to a variable.

Conclusion – Replace A Character At A Specific Index In JavaScript

In this article, we looked at how to replace a character at a given index in JavaScript.

We also saw how to replace multiple characters at a given index.

Replacing characters in JavaScript can be done using the substring(), split() and join() methods.

Which method you choose to use depends on your particular requirements.

I hope this article has been helpful. Thank you for reading!

Leave a Reply