JavaScript endsWith() 判断字符串是否以XXX结尾

endsWith

endsWith() 方法用来判断当前字符串是否是以指定的子字符串结尾的。

如果传入的子字符串在搜索字符串的末尾则返回 true,否则将返回 false。

endsWith() 方法对大小写敏感。

let str = "Hello world, this is string test";
let res1 = str.endsWith("test")
let res2 = str.endsWith("Test")
let res3 = str.endsWith("rest")
console.log(res1,res2,res3)

运行结果:

在这里插入图片描述

语法

string.endsWith(searchvalue, length)

参数值

参数描述
searchvalue必需,要查找的子字符串。
length设置字符串的长度。默认值为原始字符串长度 string.length。

返回值

类型描述
Boolean如果字符串以指定的值结尾返回 true,否则返回 false。

例子

检查字符串是否以“string”结尾,假设字符串长度为27个字符:

let str = "Hello world, this is string test";
let res1 = str.endsWith("string",27)
let res2 = str.endsWith("String",27)
let res3 = str.endsWith("string",8)
console.log(res1,res2,res3)

运行结果:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4p2otiQU-1643250506235)(C:\Users\mayn\AppData\Roaming\Typora\typora-user-images\image-20220126173422755.png)]

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值