JS—字符串的includes ( )

372 阅读1分钟

1.用法概述

includes ( )判断指定的一个字符串在当前字符串中是否存在,返回值是boolean,如果存在则返回 true,否则返回 false

注意:该方法区分大小写

2.语法

str.includes(strFind,indexFind)

1)参数

strFind——需要判断是否存在的字符串

indexFind——可选。指定开始搜索字符串的索引位置。如果索引值indexFind为负数,则从 arr.length + indexFind 的索引开始往后搜索元素。

2)返回值

返回值是boolean,如果当前字符串中包含需要查找的字符串,则返回 true;否则返回 false

3.案例用法

1、判断指定的字符串在当前字符串中是否存在

const str= "Hello , How are you";
console.log(str.includes("How"))   //true
console.log(str.includes("HelloWorld"))   //false
console.log(str.includes("hello"))  //false,str.includes()区分大小写

2、指定开始搜索字符串的索引位置

//从索引值为5的位置开始判断原字符串"Hello , How are you"是否包含"you"字符串
const str= "Hello , How are you";
console.log(str.includes("you",5))  //true

developer.mozilla.org/zh-CN/docs/…