Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 15:33:06
Original
1492 people have browsed it

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>
Copy after login

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>

Copy after login

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()

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!