endwith函数怎么使用

不言
不言原创
2021-04-21 17:22:227136浏览

endwith函数用于检查给定的字符串是否以指定字符串的字符结尾,该函数的使用语法是“str.endsWith(searchString,length)”。

本文操作环境:Windows7系统、javascript1.8.5版、Dell G3电脑。

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

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

以上就是endwith函数怎么使用的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。