JavaScript substring () method
Definition and Usage
substring () method is used to extract the string in two intermediary between the specified character index.
substring () method returns the substring includes characters at the beginning, but not including the character at the end.
grammar
string.substring(from, to)
参数 | 描述 |
---|---|
from | 必需。一个非负的整数,规定要提取的子串的第一个字符在 string Object 中的位置。 |
to | 可选。一个非负的整数,比要提取的子串的最后一个字符在 string Object 中的位置多 1。 如果省略该参数,那么返回的子串会一直到字符串的结尾。 |
Browser Support
All major browsers support substring () method
Examples
Examples
In this example, we will use the substring () to extract some characters from a string ::
<script>
var str="Hello world!";
document.write(str.substring(3)+"<br>");
document.write(str.substring(3,7));
</script>
var str="Hello world!";
document.write(str.substring(3)+"<br>");
document.write(str.substring(3,7));
</script>
The above code output:
lo world!
lo w
lo w
try it"
JavaScript String Object