JavaScript 判断字符串是否包含指定字符

JavaScript About 493 words

ES6 之前

indexOf(),返回-1表示不包含该字符串。

var str = "Hello world";
var result = str.indexOf("world");
console.log(result) // 6

ES6

提供了三种新方法。

includes()

返回布尔值,表示是否找到了参数字符串。

var str="Hello world";
var result=str.includes("world");
console.log(result) // true

startsWith()

返回布尔值,表示参数字符串是否在原字符串的头部。

var str="Hello world";
var result=str.startsWith("Hello");
console.log(result) // true

endsWith()

返回布尔值,表示参数字符串是否在原字符串的尾部。

var str="Hello world";
var result=str.endsWith("world");
console.log(result) // true
Views: 1,013 · Posted: 2022-07-04

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh