JavaScript | 字符串substring()方法 (JavaScript | String substring() Method)
The String.substring() method in JavaScript is used to return a part of the string. The substring() extracted is from the given start and end index of the string.
JavaScript中的String.substring()方法用于返回字符串的一部分。 提取的substring()来自给定的字符串开始和结束索引。
Syntax:
句法:
string.substring(startIndex, endIndex);
Parameter(s):
参数:
The method accepts two parameters, they are the starting and ending index of the substring string which is to be extracted.
该方法接受两个参数,它们是要提取的子字符串字符串的开始和结束索引。
Return value:
返回值:
The return type of this method is string which is the substring output of the given string.
此方法的返回类型是string ,它是给定字符串的子字符串输出。
Browser support: Chrome, Internet explorer, Mozilla, Safari, Opera mini.
浏览器支持: Chrome,Internet Explorer,Mozilla,Safari,Opera mini。
Example 1:
范例1:
<script>
document.write("Hello".substring(1,3) + "<br/>");
document.write("Hello".substring(0,2) + "<br/>");
document.write("Hello".substring(0,5) + "<br/>");
document.write("Hello".substring(0,8) + "<br/>"); //no error
document.write("Hello World!".substring(6,12) + "<br/>");
</script>
Output
输出量
el
He
Hello
Hello
World!
Example 2:
范例2:
<script>
str = "www.includehelp.com";
document.write(str.substring(0,3) + "<br/>");
document.write(str.substring(4,15) + "<br/>");
document.write(str.substring(16,19) + "<br/>");
document.write(str.substring(0) + "<br/>");
document.write(str.substring(4) + "<br/>");
document.write(str.substring(16) + "<br/>");
</script>
Output
输出量
www
includehelp
com
www.includehelp.com
includehelp.com
com
翻译自: https://www.includehelp.com/code-snippets/string-substring-method-with-example-in-javascript.aspx