JavaScript includes() method

JavaScript: String includes() method

This JavaScript tutorial explains with syntax and examples how to use the string includes() method.

describe

In JavaScript, includes() is a string method used to determine whether a substring is found in a string. Since the includes() method is a method of the String object, it must be called through a specific instance of the String class.

grammar

In JavaScript, the syntax of the includes() method is:

string.includes(substring [, start_position]);

parameter

substring This is the substring you want to search for in string .

start_position Optional. It is the position in the string where the search begins. The first position in string is 0. If this parameter is not provided, the search will start from the beginning of string .

return value

The includes() method returns true if the substring is found in string . Otherwise, it returns false .

Notice

  • The contains() method performs a case-sensitive search.
  • The contains() method does not change the value of the original string .

example

Let's see an example of how to use the contains() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.includes('e'));

In this example, we declare a variable called totn_string which is assigned the string value of "TechOnTheNet". We then called the includes() method of the totn_string variable to determine if the substring was found in totn_string .

For demonstration purposes, we have written the output of the includes() method to the web browser console log to show what the contains() method returns.

The following will be output to the web browser console log :

true

In this example, the includes() method returns true because the substring "e" was found in "TechOnTheNet" when performing a case-sensitive search.

Specifies the starting position parameter

You can change where in the string the search will start by providing a start_position parameter to the includes() method .

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.includes('e',11));

The following will be output to the web browser console log :

false

In this example, we set the start_position parameter to the value 11. This means that the search will start looking for the value "e" at position 11 in the string "TechOnTheNet". So in this case the includes() method returns false because the substring 'e' was not found.

specify multiple characters as a substring

Finally, the includes() method can search for multiple characters in a string.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.includes('The'));

The following will be output to the web browser console log :

true

In this example, the includes() method returns true because the substring 'The' was found in the string 'TechOnTheNet'.

 Well, the above is all the content of this article, I hope it will be helpful to you, and I hope you will give a lot of support to Code Farmer's House . Your support is the driving force for my creation! I wish you all a happy life!

Guess you like

Origin blog.csdn.net/wuxiaopengnihao1/article/details/126941955