How to use substr() method In Javascript

The substr() method can be used to split a string into a small sub string then add the character and after that get the removed substring from the original string to completer it.

Syntax:

initialStr.substr(startInd, endInd) + 'extraCharacter' + 
initialStr.substr(indexAfterWhichTheStringWasRemoved);

Example: The below code explains the use of the substr() method to add a character to the string in JavaScript.

Javascript
const str1 = "GeesforGeeks"
const updatedStr1 = str1.substr(0, 3) + 'k' + str1.substr(3);
const str2 = "JavaSript"
const updatedStr2 = str2.substr(0, 5) + 'c' + str2.substr(5);
console.log(updatedStr1, updatedStr2);

Output
w3wiki JavaScript

Add Characters to a String in JavaScript

In JavaScript, A string is a combination of multiple characters joined together to form a meaningful word. It can be a name or anything else. You can add an extra character to a string after it is defined to complete it or to practice adding characters.

There are several ways available in JavaScript to achieve this task as listed below:

Table of Content

  • Using β€˜+’ operator
  • Using the concat() method
  • Using template literals
  • Using slice() method
  • Using substring() method
  • Using substr() method
  • Using Array.prototype.join() Method

Similar Reads

Using β€˜+’ operator

The + operator can be used to concat or add an extra character to the string by using it in between the string and the character to be added....

Using the concat() method

The string concat() method in JavaScript is used to merge two or more strings and returns a new string that is the result of a combination of the specified string. We can also use it to add a character to the string....

Using template literals

The template literals can also be used to add character to the string by storing the character into a variable and insert it at any position in the string using the template literal syntax....

Using slice() method

The slice() method can be used to add a character to a string by removing the string after the index where you want to add the character and then again use the slice() method with the original string to get back the removed string....

Using substring() method

The substring() method of the string can also be used to add a character to a string in the same way we use the slice() method by removing and adding the string to add the character at specified position....

Using substr() method

The substr() method can be used to split a string into a small sub string then add the character and after that get the removed substring from the original string to completer it....

Using Array.prototype.join() Method

The Array.prototype.join() method can be utilized to add a character to a string. By converting the string to an array of characters, inserting the new character at the desired position, and then joining the array back into a string, you can achieve this task....