Last updated: Mar 1, 2024
Reading timeยท5 min
To replace a character at a specific index in a string:
String.slice()
method to get the part before the character.String.slice()
method to get the part after the character.function replaceCharacter(string, index, replacement) { return ( string.slice(0, index) + replacement + str.slice(index + replacement.length) ); } const str = 'bobby'; const result1 = replaceCharacter(str, 2, '_'); console.log(result1); // ๐๏ธ bo_by const result2 = replaceCharacter(str, 2, '!@'); console.log(result2); // ๐๏ธ bo!@y
The String.slice() method extracts a section of a string and returns it, without modifying the original string.
The String.slice()
method takes the following arguments:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
slice()
method doesn't change the original string. The method returns a new string containing a part of the original string.The first call to the String.slice()
method returned a part of the string
before the character that is to be replaced.
The second call to the method returned the part of the string after the character to be replaced.
const str = 'bobby'; const index = 2; const replacement = '_'; // ๐๏ธ bo console.log(str.slice(0, index)); // ๐๏ธ by console.log(str.slice(index + replacement.length));
In the example, we replace the character at index 2
, which is a b
with an
underscore _
.
index
argument in the second call to the String.slice()
method.This is useful when you need to replace multiple characters in the string.
function replaceCharacter(string, index, replacement) { return ( string.slice(0, index) + replacement + str.slice(index + replacement.length) ); } const str = 'bobby'; const index = 2; const replacement = '!@'; // ๐๏ธ bo!@y console.log(replaceCharacter(str, index, replacement));
The start index is inclusive, so we have to add at least 1 to the index
of the
character to be replaced.
If you only need to replace a single character in the string, you don't have to
use the length of the replacement string, you can directly add 1
to the index.
function replaceCharacter(string, index, replacement) { return ( string.slice(0, index) + replacement + str.slice(index + 1) ); } const str = 'bobby'; const index = 2; const replacement = '_'; // ๐๏ธ bo_by console.log(replaceCharacter(str, index, replacement));
This approach also works if you need to replace a single character with multiple characters, starting at a specific index.
function replaceCharacter(string, index, replacement) { return string.slice(0, index) + replacement + str.slice(index + 1); } const str = 'bobby'; const index = 2; const replacement = '___'; // ๐๏ธ bo___by console.log(replaceCharacter(str, index, replacement));
We replaced the character in the string at index 2
with multiple characters.
If you need to get the index of a substring in a string, use the
String.indexOf()
method.
const str = 'bobby'; console.log(str.indexOf('o')); // ๐๏ธ 1 console.log(str.indexOf('bb')); // ๐๏ธ 2
The String.indexOf() method returns the index of the first occurrence of a substring in a string.
If the substring is not contained in the string, the method returns -1
.
If you need to replace multiple characters in the string with multiple characters, use the length of the replacement string to determine the first character of the second slice.
function replaceCharacter(string, index, replacement) { return ( string.substring(0, index) + replacement + str.substring(index + replacement.length) ); } const str = 'bobby'; const index = 2; const replacement = '%^@'; // ๐๏ธ bo%^@ console.log(replaceCharacter(str, index, replacement));
The start index n the second call of the String.substring()
method is used by
adding the index of the character to be replaced to the length of the
replacement string.
This means that we are excluding exactly as many characters from the original string as we provide in our replacement string.
Alternatively, you can use the String.split()
and Array.join()
methods.
This is a three-step process:
String.split()
method to split the string into an array of
characters.Array.join()
method to join the array without a separator.function replaceCharacter(string, index, replacement) { const arr = string.split(''); arr[index] = replacement; return arr.join(''); } // ๐๏ธ _obby console.log(replaceCharacter('bobby', 0, '_')); // ๐๏ธ b_bby console.log(replaceCharacter('bobby', 1, '_')); // ๐๏ธ b!@bby console.log(replaceCharacter('bobby', 1, '!@'));
We first used the String.split()
method to split the string into an array.
const str = 'bobby'; const arr = str.split(''); console.log(arr); // ๐๏ธ [ 'b', 'o', 'b', 'b', 'y' ]
The String.split() method takes a separator and splits the string into an array on each occurrence of the provided delimiter.
The String.split()
method takes the following 2 parameters:
Name | Description |
---|---|
separator | The pattern describing where each split should occur. |
limit | An integer used to specify a limit on the number of substrings to be included in the array. |
The next step is to use bracket notation to replace the character at the specified index.
const str = 'bobby'; const arr = str.split(''); console.log(arr); // ๐๏ธ [ 'b', 'o', 'b', 'b', 'y' ] arr[0] = '_'; console.log(arr); // ๐๏ธ [ '_', 'o', 'b', 'b', 'y' ]
The last step is to use the Array.join()
method to join the array's elements
into a string without a separator.
const str = 'bobby'; const arr = str.split(''); console.log(arr); // ๐๏ธ [ 'b', 'o', 'b', 'b', 'y' ] arr[0] = '_'; console.log(arr); // ๐๏ธ [ '_', 'o', 'b', 'b', 'y' ] const result = arr.join(''); console.log(result); // ๐๏ธ _obby
The Array.join() method concatenates all of the elements in an array using a separator.
The only argument the Array.join()
method takes is a separator
- the string
used to separate the elements of the array.
If the separator
argument is set to an empty string, the array elements are
joined without any characters in between them.
This approach is suitable when you need to replace a single character in a string at a specific index with one or more characters.
You can learn more about the related topics by checking out the following tutorials: