// 检查一个字符串是否包含另一个字符串
// 使用 includes() 方法
let str = "Hello, welcome to my world.";
let result = str.includes("welcome");
if (result) {
console.log("字符串中包含 'welcome'");
} else {
console.log("字符串中不包含 'welcome'");
}
// includes() 方法区分大小写
let result2 = str.includes("WELCOME");
if (result2) {
console.log("字符串中包含 'WELCOME'");
} else {
console.log("字符串中不包含 'WELCOME'");
}
// 如果需要忽略大小写,可以将两个字符串都转换为小写后再进行比较
let result3 = str.toLowerCase().includes("welcome".toLowerCase());
if (result3) {
console.log("字符串中包含 'welcome'(忽略大小写)");
} else {
console.log("字符串中不包含 'welcome'(忽略大小写)");
}
includes()
方法用于检查一个字符串是否包含指定的子字符串。如果包含,则返回 true
,否则返回 false
。下一篇:javascript ?.
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站