JavaScript字符串include()函数教程及⽰例
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()函数语法 (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.
“ STRING”是⼀个字符串变量或字符串⽂字,将在其中搜索SEARCH_TERM。
`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.
ʻincludes()`是我们将使⽤以下参数的函数。 该函数将根据情况返回布尔结果true和false并进⾏匹配。 如果不匹配,则在存在匹配项时将返回false,⽽将返回true。
`SEARCH_TERM` is the term we will search in the STRING which can be a string variable or string literal.
“ SEARCH_TERM”是我们将在STRING中搜索的术语,可以是字符串变量或字符串⽂字。
`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.
“ START”是搜索的起始索引号,从指定的START索引开始,搜索将从STRING开始。 START是可选的,如果未提供,则搜索将从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” inside the greeting variable with the includes() function.
我们将从⼀个简单的⽰例开始,在该⽰例中,我们将在给定的字符串中搜索⼀个简单的术语。 在此⽰例中,我们将创建⼀个字符串变
量greeting并使⽤includes()函数在greeting变量中搜索“ poftut”。
console.log(match);
//Prints true
var match = "Hello my name is poftut. You can find very good tutorials about IT on me.".includes("poftut");
console.log(match);
//Prints true
var greeting="Hello my name is poftut. You can find very good tutorials about IT on me.";
var match = greeting.includes("POFTUT.COM");
console.log(match);
//Prints false
Search Given Term In the Whole String
在整个字符串中搜索给定术语
includes() is a case insensitive function where “poftut” and “POFTUT.COM” are not the same. S
o they will not match in a search.
include()是区分⼤⼩写的函数,其中“ poftut”和“ POFTUT.COM”不同。 因此它们将在搜索中不匹配。
了解更多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” after 10th character.
includes()函数还接受搜索开始索引,在该索引之后将执⾏搜索。 在下⾯的⽰例中,我们将在第10个字符之后搜索“ poftut”。
console.log(match);
//Print to the console true
var match = greeting.includes("poftut",30);
console.log(match);
//Print to the console false
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.
indexof的用法javascript从⽰例中我们可以看到,当索引指定为10时,给定的字符串将匹配,includes()函数将返回true。 如果
将索引指定为30,它将不匹配并返回false。
⽐较include()函数结果 (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. You can find very good tutorials about IT on me.".includes("poftut");
//Evaluated as true
1 == "Hello my name is poftut. You can find very good tutorials about IT on me.".includes("poftut");
//Evaluated as true
1 == "Hello my name is poftut. You can find very good tutorials about IT on me.".includes("POFTUT.COM");
//Evaluated as false
-11 == "Hello my name is poftut. You can find very good tutorials about IT on me.".includes("POFTUT.COM");
//Evaluated as false
-1 == "Hello my name is poftut. You can find very good tutorials about IT on me.".includes("POFTUT.COM");
//Evaluated as false
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. You can find very good tutorials about IT on me.";
index=greeting.indexOf("poftut");
console.log(index);
//Output 17
index=greeting.indexOf("POFTUT.COM");
console.log(index);
//Output -1
includes() Alternative indexOf()  Function
include()替代indexOf()函数
翻译⾃: