How to use substring() method In Javascript
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.
Syntax:
initialStr.substring(startInd, endInd) + 'extraCharacter' +
initialStr.substring(indexAfterWhichTheStringWasRemoved);
Example: The below code implements the substring() method to add a character to the string.
const str1 = "GeekforGeeks"
const updatedStr1 = str1.substring(0, 4) + 's' + str1.substring(4);
const str2 = "JaaScript"
const updatedStr2 = str2.substring(0, 2) + 'v' + str2.substring(2);
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