📜  JavaScript String include()

📅  最后修改于: 2020-10-27 01:01:01             🧑  作者: Mango

JavaScript字符串include()

JavaScript字符串include()方法用于确定给定字符串是否存在指定的子字符串。这是区分大小写的方法。它返回布尔值,即true或false。如果字符串包含指定的子字符串,则返回true;否则返回false。

它不会更改原始字符串的值。

句法

以下语法表示include()方法:

string.includes(searchValue, start);

参数值

此方法的参数值定义如下:

searchValue:必填参数。它是要搜索的子字符串。

start:是可选参数。它表示在字符串开始搜索的位置。它的默认值为0。如果省略,则搜索将从字符串的初始位置开始,即从0开始。

让我们使用一些示例来理解include()方法。

例1

这是确定给定字符串是否包含指定子字符串的简单示例。在这里,我们声明一个变量str,并为其分配一个字符串值“ Welcome to the javaTpoint.com”。然后,我们使用include()方法来确定是否存在给定的子字符串(“ to”)。

在这里,我们没有定义开始搜索的位置。因此,搜索将从字符串的初始位置开始。







Hello world :):)

This is an example of using the JavaScript's string includes() method.

输出量

例2

在此示例中,我们将确定include()方法是否区分大小写。给定的字符串是“欢迎使用javaTpoint.com”。我们正在给定字符串中搜索子字符串“ TO”。

尽管给定字符串存在单词“ to”,但是该方法区分大小写,因此它将返回布尔值false。







Hello world :):)

This is an example of using the JavaScript's string includes() method.

Here, we are searching for the substring 'TO' in the given string.

输出量

例子3

在此示例中,我们定义了开始搜索的位置。因此,搜索将从指定位置开始。







Hello world :):)

This is an example of using the JavaScript string includes() method.

输出量