JavaScript slice() method
Complete String Object Reference
Definition and Usage
The slice() method extracts a part of a string and returns the extracted part
in a new string.
Syntax
|
stringObject.slice(start,end)
|
| Parameter |
Description |
| start |
Required. Specify where to start the selection. Must be a
number. Starts at 0 |
| end |
Optional. Specify where to end the selection. Must be a
number |
Tips and Notes
Tip: You can use negative index numbers to select from the end of the
string.
Note: If end is not specified, slice() selects all characters from the
specified start position and to the end of the string.
Example 1
In this example we will extract all characters from a string, starting at
position 0:
<script type="text/javascript">
var str="Hello happy world!";
document.write(str.slice(0));
</script>
|
The output of the code above will be:
Example 2
In this example we will extract all characters from a string, starting at
position 6:
<script type="text/javascript">
var str="Hello happy world!";
document.write(str.slice(6));
</script>
|
The output of the code above will be:
Example 3
In this example we will extract only the first character from a string:
<script type="text/javascript">
var str="Hello happy world!";
document.write(str.slice(0,1));
</script>
|
The output of the code above will be:
Example 4
In this example we will extract all the characters from position 6 to
position 11:
<script type="text/javascript">
var str="Hello happy world!";
document.write(str.slice(6,11));
</script>
|
The output of the code above will be:
Try-It-Yourself Demos
slice()
This shows all the methods form the examples above.
Complete String Object Reference
729,913 sites built with Wix. Make your own
Click here to design a Stunning Flash Website for Free
Wix is a revolutionary web design tool that provides anyone with the possibility to create professional and beautiful websites for free.
With e-commerce features, search engine visibility and many more professional tools, Wix is the ultimate solution for creating a spectacular site while saving tons of money.
|