JavaScript 字符串 endsWith() 方法
JavaScript 字符串 endsWith() 方法用于判断字符串是否以指定字符(子字符串)结尾。如果在字符串末尾找到指定的子字符串,则返回布尔值"true";否则返回"false"。
如果 searchString 参数是正则表达式,则此方法抛出 TypeError 异常。它区分大小写,因此会将"Hello"和"hello"视为不同的字符串。
语法
以下是 JavaScript 字符串 endsWith() 方法的语法 -
endsWith(searchString, endPosition)
参数
此方法接受两个参数:"searchString"和"endPosition"。 endPosition 参数是可选的,用于指定搜索在字符串中结束的索引(位置)。
- searchString − 要在字符串末尾搜索的字符。
- endPosition(可选) − 搜索在字符串中结束的位置。
返回值
如果字符串以指定的子字符串结尾,则此方法返回 true,否则返回 false。
示例 1
如果字符串以指定的字符结尾,则返回 true。
在此示例中,我们使用 JavaScript 字符串的 endsWith() 方法来检查字符串"TutorialsPoint"是否以指定的子字符串"Point"结尾。
<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
const str = "TutorialsPoint";
const searchString = "Point";
document.write("Original string: ", str);
document.write("<br>Search string: ", searchString);
document.write("<br>Is string '", str, "' ends with '", searchString, "'or not? ", str.endsWith(searchString));
</script>
</body>
</html>
输出
上述程序返回"true"。
Original string: TutorialsPoint Search string: Point Is string 'TutorialsPoint' ends with 'Point'or not? true
示例 2
如果字符串不以指定字符结尾,则返回 false。
以下是 JavaScript 字符串 endsWith() 方法的另一个示例。在此示例中,我们使用此方法判断字符串"Hello World"是否以指定字符串"Word"结尾。
<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
const str = "Hello World";
const searchString = "Word";
document.write("Original string: ", str);
document.write("<br>Search string: ", searchString);
document.write("<br>Is string '", str, "' ends with '", searchString, "' or not? ", str.endsWith(searchString));
</script>
</body>
</html>
输出
执行上述程序后,返回"false"。
Original string: Hello World Search string: Word Is string 'Hello World' ends with 'Word' or not? false
示例 3
如果我们将 endPosition 参数值设置为 15,它会检查以索引 15 结尾的字符串是否以指定的子字符串结尾,并忽略该字符串的其余部分。如果子字符串与 searchString 的末尾匹配(直到索引 15),则返回 true;否则,返回 false。
<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
const str = "Welcome to Tutorials Point";
let endPosition = 15;
const searchString = "Tuto";
document.write("Original string: ", str);
document.write("<br>End position: ", endPosition);
document.write("<br>Search string: ", searchString);
document.write("<br>String actual length: ", str.length);
document.write("<br>Is string '", str, "'(upto length 15) ends with '",searchString,"' or not? ", str.endsWith(searchString, endPosition));
</script>
</body>
</html>
输出
上述程序执行后将返回"true"。
Original string: Welcome to Tutorials Point End position: 15 Search string: Tuto String actual length: 26 Is string 'Welcome to Tutorials Point'(upto length 15) ends with 'Tuto' or not? true
示例 4
JavaScript String endsWith() 方法要求 searchString 参数为字符串。如果是正则表达式,则会抛出"TypeError"异常。
<html>
<head>
<title>JavaScript String endsWith() Method</title>
</head>
<body>
<script>
const str = "Tutorials Point";
const searchString = /ab+c/;
document.write("Original string: ", str);
document.write("<br>Search string: ", searchString);
try {
document.write("Is string '",str,"' is ends with '",searchString,"' or not? ", str.endsWith(searchString));
} catch (error) {
document.write("<br>", error);
}
</script>
</body>
</html>
输出
上述程序抛出了"TypeError"异常。
Original string: Tutorials Point Search string: /ab+c/ TypeError: First argument to String.prototype.endsWith must not be a regular expression

