// 检查字符串是否包含某个字符的示例代码
// 使用 includes() 方法
let str = "Hello, world!";
let char = "world";
let result = str.includes(char);
console.log(result); // 输出: true
// 解释说明:
// includes() 方法用于判断字符串中是否包含指定的子字符串,如果包含则返回 true,否则返回 false。
// 在这个例子中,我们检查字符串 "Hello, world!" 是否包含子字符串 "world",结果是 true。
// 如果需要区分大小写,includes() 是区分大小写的。
let str2 = "Hello, World!";
let char2 = "world";
let result2 = str2.includes(char2);
console.log(result2); // 输出: false
// 如果不区分大小写,可以将两个字符串都转换为小写后再进行比较
let result3 = str2.toLowerCase().includes(char2.toLowerCase());
console.log(result3); // 输出: true
上一篇:js 把大写转为小写
下一篇:js 获取cookie
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站