Home  >  Article  >  Web Front-end  >  An example of how to use JavaScript substring()_javascript skills

An example of how to use JavaScript substring()_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:33:061487browse

Definition and usage
The substring() method is used to extract characters between two specified subscripts in a string.
Grammar
stringObject.substring(start,stop)

Return value
A new string value containing a substring of stringObject whose content is all characters from start to stop-1, and whose length is stop minus start.
Description
The substring returned by the substring() method includes the character at start but does not include the character at stop.
If the parameters start and stop are equal, then this method returns an empty string (that is, a string of length 0). If start is greater than stop, the method swaps the two arguments before extracting the substring.
Tips and Notes
Important: Unlike the slice() and substr() methods, substring() does not accept negative arguments.
Explanation with examples
Example 1
In this example we will use substring() to extract some characters from a string:

<script type="text/javascript">

var str="Hello world!"
document.write(str.substring(3))

</script>

Output: lo world!

Example 2
In this example we will use substring() to extract some characters from a string:

<script type="text/javascript">

var str="Hello world!"
document.write(str.substring(3,7))

</script>

Output: lo w

The above is the usage of javaScript substring() shared with you. I hope you can try it yourself and truly master the usage of javaScript substring()

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn