• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Insert Character Into String In JavaScript

Insert Character Into String In JavaScript

July 12, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Change the Image src
  • 2.  Remove Parentheses From String Using JavaScript
  • 3.  Using JavaScript to Check if Variable is an Object
  • 4.  Remove Empty Strings from an Array in JavaScript
  • 5.  Adding CSS Important Property Using JavaScript
  • 6.  JavaScript Trunc with Math.trunc() Method
  • 7.  Using JavaScript to Check if Variable is a Number
  • 8.  Using JavaScript to Get the URL Path
  • 9.  Using JavaScript to Convert String to Boolean
  • 10.  Using JavaScript to Convert Float to Integer

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy