JavaScript中的str.endsWith()函数用于检查给定的字符串是否以指定字符串的字符结尾。下面我们就来具体看看endwith函数的使用方法。 
我们先来看一下endsWith()函数的基本语法 str.endsWith(searchString,length) endsWith()函数的第一个参数是searchString字符串,它将在给定字符串的末尾进行搜索。函数的第二个参数是length,它确定从搜索searchString开始的给定字符串的长度。 如果找到searchString,则此函数返回布尔值true,否则返回布尔值false 下面我们来看endsWith()函数具体的示例 代码如下 检查字符串str是否以day.结尾
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script>
function func() {
var str = 'It is a great day.';
var value = str.endsWith('day.');
document.write(value);
}
func();
</script>
</body>
</html> 输出结果为:true
检查字符串str是否以great结尾 <!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script>
function func() {
var str = 'It is a great day.';
var value = str.endsWith('great');
document.write(value);
}
func();
</script>
</body>
</html> 输出结果为:false
检查字符串str是否在指定的索引13处以great结尾 <!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script>
function func() {
var str = 'It is a great day.';
var value = str.endsWith('great',13);
document.write(value);
}
func();
</script>
</body>
</html> 输出结果为:true 本篇文章到这里就全部结束了,更多精彩内容大家可以关注模板之家(www.mb5.com.cn)的其他相关栏目教程!!! 以上就是endwith函数怎么使用的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |