Using the substring () or slice () method (nn4+, ie4+), the specific use of them is described below.
The prototype for substring () is:
String.substring (from, to)
The first argument from specifies the starting position of the substring in the original string (based on the 0 index); the second argument to is optional, which specifies the substring at the end of the original string (based on the 0 index), and generally, it should be larger than from, if it is omitted, The substring is then kept at the end of the original string.
What happens if the parameter from is accidentally larger than the parameter? JavaScript automatically mediates the start and end of a substring, which means that substring () always starts with the smaller of the two arguments, and ends with the larger one. Note, however, that it contains that character from the starting position, but does not contain the character at the end position.
var fullstring = "Every dog has his day."
var section = fullstring.substring (0, 4); The section is "Ever".
Section = fullstring.substring (4, 0); The section is also "Ever".
Section = fullstring.substring (1, 1); The section is a empty string.
Section = fullstring.substring (-2, 4); Section is "Ever", same as Fullstring.substring (0, 4); The prototype of Slice () is: String.slice (start, end)
The parameter start indicates the starting position of the substring and, if it is a negative number, it can be understood as the beginning of the penultimate, for example, 3, starting at the last third, and the argument end representing the ending position, which, like start, can also be negative, and its meaning to the penultimate end. The slice () parameter can be negative, so it is more flexible than substring (), but less forgiving, and if start is larger than end, it returns an empty string (example).
Another method is substr (), whose prototype is:
String.substr (start, length)
From the prototype, you can see the meaning of its arguments, start represents the starting position, and length represents the substring. JavaScript standards do not encourage the use of this method.