Table of Contents
The substring methods extracts the characters between the first param(start) and second param(last), but does not include the last.
Start : the first position from where the extraction begins. The first position is at index 0.
end : it up to (but not including) where the extraction ends.
To extract the first 10 character from a string, the syntax will be
let str = "abcdefghijkl" let tenChar = str.substring(0,10) console.log(tenChar) //abcdefghij <- FIRST 10 CHARACTER FROM THE STRING
The extraction of the character starts with the position 0 (the position is index 0 ) and ends with 10 (the position is index 9).
- If the start position is greater then the end position, the
substring()will swap the two arguments. Example (5,1) will be swap to (1,5).
- If the start or the end argument is less the 0, the will be treated as 0
substring()method will not bring as changes to the original string.