myString.slice( start, [ end ] ) in ECMAScript 262
Return a specified subsection of the string.
Arguments
name | type | description |
---|---|---|
start | Number | The index to start from. |
end | Number | [optional] The index to stop before. |
- Return Type
- String
Description
This method creates a new string and copies the characters from the start
index up to (but not including) the end
index.
If end
is omitted, the copying continues from the start
to the end of the string.
If either start
or end
are negative, they are treated as offsets from the end of the string.
If end
occurs before (or is the same as) start
, an empty string is returned.
var msg = "Hello World";
var s1 = msg.slice(0,3); // ** s1 is "Hel"
var s2 = msg.slice(6,10); // ** s2 is "Worl"
var s3 = msg.slice(-3); // ** s3 is "rld"
var s4 = msg.slice(0,-2); // ** s4 is "Hello Wor"
var s5 = msg.slice(3); // ** s5 is "lo World"