We can insert a character into a string in JavaScript by using the JavaScript String substring() method. To insert a character into a string, we will need an index position for where to put it.


someString = someString.substring(0, indexPosition) + 'r' + someString.substring(indexPosition);

Where indexPosition in the above code is the index of the character we want to insert, and ‘r’ is the new character we want to insert into the string.

Let’s see an example of this below.


Let’s say we have the following JavaScript:


var someString = "We want to insert a character __ in between the underscores.";
someString = someString.substring(0, 31) + 't' + someString.substring(31);

In the above code, someString will have the following value after applying the substring method to the original string:

We want to insert a character _t_ in between the underscores.

In the example above, we start with the first part someString.substring(0, 31). This will get all of the text up to and including the first underscore.

Since we want to insert the character before the second underscore, we use the index position of the second underscore as our indexPosition in our code above. The index position of the second underscore is 31.

Then we simply add our character to our first substring.

Finally, we get the rest of the string using the substring() method again. someString.substring(31) will get all the characters starting at position 31, the second underscore, until the end of the string.

We can put our code into a function to make reusing this code very simple. Our function will simply take three parameters, the string as the first one, the position of the character that we want to insert our character right before, and the third parameter is the character itself. The function will return the new string, with the character added to it.

function insertCharToString(str,indexPos,char){
  return str.substring(0, indexPos) + char + str.substring(indexPos);
};

Here is the code for our function with the same example from above using it.

function insertCharToString(str,indexPos,char){
  return str.substring(0, indexPos) + char + str.substring(indexPos);
};

var someString = "We want to insert a character __ in between the underscores.";
console.log(insertCharToString(someString,31,'t'));

#Output
We want to insert a character _t_ in between the underscores.

Note that we could have also done this using the JavaScript String slice() method.

function insertCharToString(str,indexPos,char){
  return str.slice(0, indexPos) + char + str.slice(indexPos);
};

var someString = "We want to insert a character __ in between the underscores.";
console.log(insertCharToString(someString,31,'t'));

#Output
We want to insert a character _t_ in between the underscores.

This code will provide the same results as the substring method did above.

Using JavaScript to Insert a String Into Another String In JavaScript

In this example, instead of inserting a character into a string, we will insert a string. The code to do this is actually the exact same as the function we have above. We will just change the name of the function. Let’s take a look.

function insertStringIntoString(str,indexPos,str2){
  return str.substring(0, indexPos) + str2 + str.substring(indexPos);
};

var someString = "We want to insert a string __ in between the underscores.";
console.log(insertStringIntoString(someString,28,"this is another string"));

#Output
We want to insert a string _this is another string_ in between the underscores.

Hopefully this article has been useful for you to learn how to insert a character into a string in JavaScript.

Learn more. 

Categorized in:

JavaScript,

Last Update: May 3, 2024