// 检查字符串是否包含某个子字符串
const str = "Hello, world!";
const substr = "world";
if (str.includes(substr)) {
console.log("字符串包含 'world'");
} else {
console.log("字符串不包含 'world'");
}
// includes() 方法用于判断字符串中是否包含指定的子字符串,
// 如果包含则返回 true,否则返回 false。
str.includes(substr):这是 JavaScript 中用来检查一个字符串是否包含另一个子字符串的方法。它返回一个布尔值 (true 或 false)。"Hello, world!" 是否包含子字符串 "world",并根据结果输出相应的信息。如果你需要检查数组中是否包含某个元素,可以使用 Array.prototype.includes() 方法。例如:
const arr = [1, 2, 3, 4, 5];
if (arr.includes(3)) {
console.log("数组包含数字 3");
} else {
console.log("数组不包含数字 3");
}
arr.includes(3):这里我们检查数组 [1, 2, 3, 4, 5] 是否包含数字 3,并根据结果输出相应的信息。上一篇:js contains方法
下一篇:js 获取今天的日期
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站