substr string
字符串substr()方法 (String substr() Method)
substr() method is a string method in JavaScript, it is used to extract a part of the string (substring) from the given string.
substr()方法是JavaScript中的字符串方法,用于从给定的字符串中提取一部分字符串(子字符串)。
Syntax:
句法:
String.substr(start, [length]);
Here,
这里,
String is the main string.
字符串是主要字符串。
start is the initial index from where we have to extract the part of the string.
start是我们必须从中提取字符串部分的初始索引。
length is an optional parameter, it defines the total number of characters to be extracted from the given index.
length是一个可选参数,它定义了要从给定index中提取的字符总数。
Examples:
例子:
str = "IncludeHelp is made for students.";
start = 10
length = None
Output:
"p is made for students."
Input:
str = "IncludeHelp is made for students.";
start = 0
length = 11
Output:
"IncludeHelp"
Code:
码:
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
var str = "IncludeHelp is made for students.";
var substring ="";
substring = str.substr(10);
document.write(substring + "<br>");
substring = str.substr(0,11);
document.write(substring + "<br>");
substring = str.substr(12,7);
document.write(substring + "<br>");
</script>
</body>
</html>
Output
输出量
p is made for students.
IncludeHelp
is made
翻译自: https://www.includehelp.com/code-snippets/string-substr-method-with-example-in-javascript.aspx
substr string