您好,欢迎来到榕意旅游网。
搜索
您的当前位置:首页endwith函数怎么使用

endwith函数怎么使用

来源:榕意旅游网
JavaScript中的str.endsWith()函数用于检查给定的字符串是否以指定字符串的字符结尾。下面我们就来具体看看endwith函数的使用方法。

JavaScript

我们先来看一下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

本篇文章到这里就全部结束了,更多精彩内容大家可以关注Gxl网的其他相关栏目教程!!!

Copyright © 2019- nryq.cn 版权所有

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务