Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js contains

作者:死亡之神   发布日期:2026-02-04   浏览:34

// 检查字符串是否包含某个子字符串

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 中用来检查一个字符串是否包含另一个子字符串的方法。它返回一个布尔值 (truefalse)。
  • 在这个例子中,我们检查字符串 "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 获取今天的日期

大家都在看

js 数组打乱顺序

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站