JavaScript 字符串判断冒号后是否包含 '20 00'
可以使用字符串的 indexOf() 方法来判断字符串中是否包含指定的子字符串。具体实现步骤如下:
- 首先使用 indexOf() 方法找到字符串中':' 的索引位置。
- 如果找到了':',则使用 indexOf() 方法在该索引位置之后的子字符串中查找'20 00',并返回结果。
- 如果找到了'20 00',则表示':' 后的字符串开始含有'20 00',返回 true。
- 如果没有找到'20 00',或者没有找到':',则表示不满足条件,返回 false。
以下是一个具体的实现示例:
function checkString(str) {
// 找到':' 的索引位置
var colonIndex = str.indexOf(':');
// 如果找到了':',则在该索引位置之后的子字符串中查找'20 00'
if (colonIndex !== -1) {
var subStr = str.substring(colonIndex + 1);
if (subStr.indexOf('20 00') !== -1) {
return true;
}
}
// 不满足条件,返回 false
return false;
}
// 示例用法
console.log(checkString('abc:def 20 00')); // true
console.log(checkString('abc:def 20')); // false
console.log(checkString('abc:def')); // false
console.log(checkString('abc')); // false
在上述示例中,checkString() 函数接受一个字符串作为参数,然后根据条件判断返回 true 或 false。示例用法中分别测试了满足条件和不满足条件的情况。
原文地址: https://www.cveoy.top/t/topic/qqm9 著作权归作者所有。请勿转载和采集!