. Advertisement .
..3..
. Advertisement .
..4..
Sometimes you may want to insert String at specific Index of another String in JS but have no idea, so please take a few minutes to read this article. Here we have compiled some possible Methods that you can try. We will first learn some concepts before starting with the methods.
How to insert String at specific Index of another String in JS?
String has the role of representing and manipulating a string of characters. In particular, strings are handy for holding data that can be described as text.
Index in JavaScript is a value that represents the position or sequence number of an element in iterable (objects with many elements such as arrays, strings, …) containing it. By using the element’s index, we can access and get the value of that element or change its value.
Method 1: Use the splice() method
The first method you can try to insert String at specific Index of another String in JS is the splice() method.
Syntax:
splice(start, deleteCount)
Parameters:
- start: index to start changing the array.
- deleteCount: an integer that indicates the number of elements to delete start in the array.
The splice function replaces one or more elements of the array with one or more other elements, keeping in mind that the number of elements removed may be less than the number of elements added, and vice versa.
Splice javascript will change the elements in the array by removing or replacing existing elements or adding new elements to the specified position.
If you want to refer to it with a simple example, check out the example below:
function insertStringAtSpecific() {
let origStr = "ITtutoria";
let strToAdd = "-";
let indexSpec = 2;
origStr = origStr.split('');
origStr.splice(indexSpec, 0, strToAdd);
newStr = origStr.join(''); // IT-tutoria
}
Method 2: Prototype the slice() into String
To insert String at specific Index of another String in JS you can also try protype the slice() into String.
Syntax:
slice(indexStart, indexEnd)
Parameters:
- indexStart: the index of the first character to include in the returned substring.
- indexEnd: the index of the first character to exclude from the returned substring.
Given the index you want to insert the substring into, you can use it to break the current string into 2 substrings. Not only that, between 2 broken strings you can include a substring.
For example:
function insertStringAtSpecific() {
const origStr = 'ITtutoria'
const newStr = origStr.slice(0, 2) + "-" + origStr.slice(2);
console.log(newStr) // IT-tutoria
}
Txt1 is already set to ‘ITtutoria
‘, and if you want ‘-‘ to be inserted between ‘IT’ and ‘tutoria’, you need to call slice with index starting 0 and ending 2. To extract the substring from index 2 to the end of the string, you need to call slice again. Concatenate strings with +; you get the result ‘IT-tutoria
‘.
Conclusion
We hope the above methods are helpful to you. Please write your comment below the comment section if you know other ways to insert String at specific Index of another String in JS. Thank you for reading!
Read more:
Leave a comment