Insert Character into String JavaScript
String concept is very important in JavaScript, so if you are strong in it, you can do all string operations easily. When generating, such as building URLs or constructing complex strings, you might need to insert variables at specific positions within a string. So, in this article, we will see how to Insert Character into String JavaScript
3 Ways to insert character into a string
1. Using substring() method:
In this method, we’ll create a JS function with 3 parameters (String, index, Character). Inside the function, we’ll simply modify the String by using the substring() method and return it. Let’s see the example:
///Creating functions
function insertChar(str, index, char) {
return str.substring(0, index) + char + str.substring(index);
}
//Calling the function
var modifiedStr = insertChar("FriendsForever", 7, "-");
//log the variable
console.log(modifiedStr); // Output: Friends-Forever
Steps:
- In this above example, we have created the Javascript function “
insertChar
” with three parameters (str, index, char). The first parameter takes a string, the second takes an index, and the third takes a character. - “
str.substring(0, index)
” – Inside the function, we use thesubstring()
method of (str) from the beginning (index 0) up to the specified index (index) - “
+ char +
” – Then concatenates the character (char) that needs to be inserted into the string using ‘+
‘ concatenation operator . - “
str.substring(index)
” – It retrieves a substring of str starting from the specified index (index) and continuing until the end. - Then we return the concatenation of the three substrings: the substring before the insertion point, the inserted character, and the substring after the insertion point.
- Finally, we call the JavaScript function “
insertChar
” by passing three parameters (“FriendsForever”, 7, “-“). In the first parameter, you must pass the string in which you want to insert the character. In the second parameter, you need to pass the index value of which index you are going to insert the character. In the third parameter, you need to pass the character you are going to add
2. Using split() ,splice(),& join() method:
In this method, we’ll convert the original string into an array by using the split() method, insert the character into the string at the specified index position using the splice() method, and then convert the array to the string join(”) method.
//Creating funciton
function insertCharacter(str, index, char) {
var arr = str.split('');
arr.splice(index, 0, char);
return arr.join('');
}
//call the function
var originalString = "I am a Student";
var modifiedString = insertCharacter(originalString, 6, " good");
//log the variable
console.log(modifiedString);// I am a good Student
Steps:
- In this function, we have created the function “
insertCharacter
“. It takes three parameters, (str) which is the original string, (index) which is the position at which the character will be inserted, and (char) which is the character to be inserted. - Inside the function, we have converted the original string into an array of characters using the
split('')
method. It will split the string into individual characters and stores them in the variable ‘arr’. - Then we use the
splice()
method on the array (arr) to insert (char) at the specified (index) position. Here, we passed three arguments:” index ” at which the insertion should happen, “0” indicating that no elements should be removed, and char (the element to be inserted). - After the splice operation, we have modified the array ‘arr’ now contains the inserted character at the specified ‘index’.
- Then converted the array arr into the string using the join(”) method.
- Finally, we call the function insertCharacter() by passing three parameters (String, index, char).
Conclusion
These are common methods to insert a character into a string in JavaScript. Each approach has its advantages, and the choice depends on your specific use case and coding style preferences.