js insert string at specified position of string
Using stringsslice
method
Definition and usage
The slice(start, end) method extracts a part of the string and returns the extracted part as a new string.
Use start(Include
) and end(Not included
) parameter to specify the part of the string extract.
parameter | describe |
---|---|
start | must. The first character position is 0 for the starting subscript of the segment to be intercepted. If it is a negative number, it starts to intercept from the tail. |
end | Optional. The subscript at the end of the segment to be captured immediately. If this parameter is not specified, the substring to be extracted includes a string from start to the end of the original string. If the parameter is a negative number, it specifies the position from the end of the string. slice(-2) means to extract the penultimate element in the original array to the last element (including the last element). |
The code is as follows:
// souece The original string start position to be intercepted newStr character to be insertedinsertStr(source, start, newStr) { return (0, start) + newStr + (start) } // use('20220808', 4, '-') // 2020-0808
Supplement: js insert strings with specified positions for strings
js Insert strings at a specified location for strings
//use('202005',4,'/') ==================== //method://soure original string//start location//newStr String to be insertedinsertStr(soure, start, newStr){ return (0, start) + newStr + (start); }, ==================== //result2020/05
This is the article about JS inserting strings at the specified position of strings. For more related contents of JS inserting strings, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!