JavaScript provides the includes() function in order to search a given string for a specific substring. There are also other methods in order to search a string array for specific string too.

JavaScript提供了includes()函数,以便在给定的字符串中搜索特定的子字符串。 还有其他方法也可以在字符串数组中搜索特定的字符串。

(includes() Function Syntax)

includes() function is provided by a string variable or strings literal in order to find given search term in the given string.

includes()函数由字符串变量或字符串文字提供,以便在给定的字符串中找到给定的搜索词。

STRING.icludes(SEARCH_TERM,START);

`STRING` is a string variable or string literal where the SEARCH_TERM will be searched.

`includes()` is the function we will use with the following parameters. This function will return boolean results true and false according to the situation and match. If it is not matched it will return false where there is a match it will return true.

`SEARCH_TERM` is the term we will search in the STRING which can be a string variable or string literal.

`START` is the start index number of the search wherefrom the specified START index the search will start in the STRING. The START is optional where if it is not provided the search will start from the beginning of the STRING.

(Search Given Term In the Whole String)

We will start with a simple example where we will search for a simple term in the given string. In this example, we will create a string variable greeting and search the “poftut.com” inside the greeting variable with the includes() function.

我们将从一个简单的示例开始,在该示例中,我们将在给定的字符串中搜索一个简单的术语。 在此示例中,我们将创建一个字符串变量greeting并使用includes()函数在greeting变量中搜索“ poftut.com”。

var greeting="Hello my name is poftut.com. You can find very good tutorials about IT on me.";

var match = greeting.includes("poftut.com");

console.log(match);
//Prints true



var match = "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("poftut.com");

console.log(match);
//Prints true



var greeting="Hello my name is poftut.com. You can find very good tutorials about IT on me.";

var match = greeting.includes("POFTUT.COM");

console.log(match);
//Prints false

script function带参 script include_python

Search Given Term In the Whole String 在整个字符串中搜索给定术语

includes() is a case insensitive function where “poftut.com” and “POFTUT.COM” are not the same. So they will not match in a search.

include()是区分大小写的函数,其中“ poftut.com”和“ POFTUT.COM”不同。 因此它们将在搜索中不匹配。

LEARN MORE  Python String find() Function Tutorial with Examples

了解更多Python String find()函数示例教程

在字符串的指定部分中搜索给定术语(Search Given Term In the Specified Part Of The String)

includes() function also accepts the search start index where the search will be performed after that index. In the following example, we will search the term “poftut.com” after 10th character.

includes()函数还接受搜索开始索引,在该索引之后将执行搜索。 在下面的示例中,我们将在第10个字符之后搜索“ poftut.com”。

var greeting="Hello my name is poftut.com. You can find very good tutorials about IT on me.";

var match = greeting.includes("poftut.com",10);

console.log(match);
//Print to the console true

var match = greeting.includes("poftut.com",30);

console.log(match);
//Print to the console false

script function带参 script include_python_02

Search Given Term In the Specified Part Of The String 在字符串的指定部分中搜索给定术语

We can see from the examples that when the index is specified as 10 the given string will match and includes() function will return true. If we specify the index as 30 it will not match and return false.

从示例中我们可以看到,当索引指定为10时,给定的字符串将匹配,includes()函数将返回true。 如果将索引指定为30,它将不匹配并返回false。

(Comparing includes() Function Result)

As includes() function return boolean values like true and false we can compare these results with numbers like 1 and -1 which are related to boolean logic in JavaScript. -1 represents false on the other hand 1 represents true.

当include()函数返回布尔值(如true和false)时,我们可以将这些结果与与JavaScript中的布尔逻辑相关的数字(如1和-1)进行比较。 -1代表假,而1代表真。

"Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("poftut.com");
//Evaluated as true
1 == "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("poftut.com");
//Evaluated as true
1 == "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("POFTUT.COM");
//Evaluated as false
-11 == "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("POFTUT.COM");
//Evaluated as false
-1 == "Hello my name is poftut.com. You can find very good tutorials about IT on me.".includes("POFTUT.COM");
//Evaluated as false

script function带参 script include_字符串_03

Comparing includes() Function Result 比较include()函数结果

include()替代indexOf()函数(includes() Alternative indexOf()  Function)

indexOf() function is an alternative to the includes() function where the starting index number of the given term is returned. If there is not match -1 will be returned.

indexOf()函数是includes()函数的替代方法,其中返回给定术语的起始索引号。 如果不匹配,将返回-1。

var greeting="Hello my name is poftut.com. You can find very good tutorials about IT on me.";

index=greeting.indexOf("poftut.com");

console.log(index);
//Output 17

index=greeting.indexOf("POFTUT.COM");

console.log(index);
//Output -1

script function带参 script include_script function带参_04

includes() Alternative indexOf()  Function include()替代indexOf()函数

翻译自: https://www.poftut.com/javascript-string-includes-function-tutorial-with-examples/