字符串startswith
字符串startsWith()方法 (String startsWith() Method)
startsWith() method is a string method in JavaScript, it is used to check whether a string starts with a specified substring or not.
startsWith()方法是JavaScript中的字符串方法,用于检查字符串是否以指定的子字符串开头。
It returns true – if string starts with a specified substring, and it returns false – if string does not start with the specified substring.
它返回true -如果字符串以指定的子,它返回false -如果字符串不与指定的字符串开始。
Syntax:
句法:
String.startsWith(substring);
Examples:
例子:
Input:
str = "IncludeHelp is made for students.";
substring = "IncludeHelp"
//function call
str.startsWith(substring);
Output:
True
Input:
str = "IncludeHelp is made for students.";
substring = "Hello"
//function call
str.startsWith(substring);
Output:
False
Code:
码:
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
var str = "IncludeHelp is made for students.";
var substring = "IncludeHelp";
if(str.startsWith(substring)){
document.write(str + " starts with " + substring + "<br>");
}
else{
document.write(str + " does not start with " + substring + "<br>");
}
substring = "Hello";
if(str.startsWith(substring)){
document.write(str + " starts with " + substring + "<br>");
}
else{
document.write(str + " does not start with " + substring + "<br>");
}
</script>
</body>
</html>
Output
输出量
IncludeHelp is made for students. starts with IncludeHelp
IncludeHelp is made for students. does not start with Hello
翻译自: https://www.includehelp.com/code-snippets/string-startsWith-method-with-example-in-javascript.aspx
字符串startswith