Javascript教程
JavaScript基础
JavaScript Objects
JavaScript BOM
JavaScript DOM
JavaScript OOP
JavaScript Cookies
JavaScript事件
JavaScript异常
JavaScript常用

JavaScript include()

JavaScript字符串 includes()方法用于确定给定字符串中是否存在指定的子字符串。这是区分大小写的方法。它返回布尔值, true false 。如果字符串包含指定的子字符串,则返回true;否则,返回false。
它不会更改原始字符串的值。

语法

以下语法表示 includes()方法:
string.includes(searchValue, start);

参数值

此方法的参数值定义如下:
searchValue: 。这是要搜索的子字符串。
start:这是一个可选参数。它表示在字符串中开始搜索的位置。其默认值为0。如果省略,则搜索将从字符串的初始位置开始,即从 0 开始。
让我们理解 includes()方法。

Example1

这是确定给定字符串是否包含指定子字符串的简单示例。在这里,我们声明一个变量 str ,并为其分配一个字符串值 'Welcome to the lidihuo.com'。然后,我们使用 includes()方法确定是否存在给定的子字符串(" ")。
在这里,我们没有定义开始搜索的位置。因此,搜索将从字符串的初始位置开始。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> Hello world :):) </p>
<p> This is an example of using the JavaScript's string includes() method. </p>
<script>
let str = "Welcome to the lidihuo.com";
document.write(" <b> The given string is: </b>", str);
document.write("<br>");
let res = str.includes('tO');
document.write(" <b> The result is: </b> ", res);
</script>
</body>
</html>
输出

Hello world :):)

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

Example2

在此示例中,我们确定 includes()方法是否区分大小写。给定的字符串为 "欢迎使用lidihuo.com" 。我们正在给定字符串中搜索子字符串 'TO'
尽管给定字符串中存在单词 'to',但是方法区分大小写,因此它将返回布尔值 false
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> Hello world :):) </p>
<p> This is an example of using the JavaScript's string includes() method. </p>
<p> Here, we are searching for the substring <b> 'TO' </b> in the given string. </p>
<script>
let str = "Welcome to the lidihuo.com";
document.write(" <b> The given string is: </b>", str);
document.write("<br>");
let res = str.includes('TO');
document.write(" <b> The result is: </b> ", res);
</script>
</body>
</html>
输出

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.

Example3

在此示例中,我们定义了开始搜索的位置。因此,搜索将从指定位置开始。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> Hello world :):) </p>
<p> This is an example of using the JavaScript string includes() method. </p>
<script>
let str = "Welcome to the lidihuo.com";
document.write(" <b> The given string is: </b>", str);
document.write("<br>");
let res = str.includes('the', 10);
document.write(" <b> The result of str.includes('the', 10) is : </b> ", res);
</script>
</body>
</html>
输出

Hello world :):)

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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4