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

js 字符串包含判断

作者:尒忸旳迣鎅゛沬卜鋽旳凄涼   发布日期:2026-04-11   浏览:56

// 判断字符串是否包含某个子字符串

// 使用 includes() 方法
let str = "Hello, world!";
let substr = "world";
let result = str.includes(substr);
console.log(result); // 输出: true

// includes() 方法返回一个布尔值,表示字符串中是否包含指定的子字符串。
// 如果包含,则返回 true;否则返回 false。

// 使用 indexOf() 方法
let index = str.indexOf(substr);
console.log(index !== -1); // 输出: true

// indexOf() 方法返回指定子字符串在字符串中首次出现的位置。
// 如果没有找到子字符串,则返回 -1。因此可以通过判断返回值是否为 -1 来确定是否包含子字符串。

上一篇:js 判断字符串包含

下一篇:js jwt

大家都在看

js 数组打乱顺序

js 两个数组取交集

js 数组对象排序

js 对象数组排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

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

Laravel 中文站