Check a string that MUST contain another string
本问题已经有最佳答案,请猛点这里访问。
我想检查字符串
我试过:
| 1 2 3 4 5 6 7 | var a ="helloworld"; var b ="wold"; if(a.indexOf(b)) { document.write('yes'); } else { document.write('no'); } | 
输出是肯定的,这不是我期望的输出,因为字符串B(wld)不完全包含在字符串A(helloworld)---wld v.s.world中
有没有建议检查字符串?
阅读文档:mdc string.indexof:。
如果没有找到针,
快乐编码。
您需要测试结果是否为-1。-1表示不匹配,但在布尔值意义上计算为真。
| 1 2 3 4 5 6 7 | var a ="helloworld"; var b ="wold"; if(a.indexOf(b) > -1) { document.write('yes'); } else { document.write('no'); } | 
如果找不到匹配项,
| 1 | if (a.indexOf(b) != -1) | 
你需要用
你可能想用这个
| 1 | if(a.indexOf(b) != -1) | 
这是因为如果找不到值,
| 1 | if(a.indexOf(b) != -1) { |