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.
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