javascript 检查变量是否存在字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7887167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 01:39:02  来源:igfitidea点击:

check variable if char exist or not

javascript

提问by Rahul Singh

Possible Duplicate:
How to tell if a string contains a certain character in javascript?

可能的重复:
如何判断一个字符串是否包含 javascript 中的某个字符?

Suppose I have a string in variable ex. var name="Stackoverflow". I want to check in this string if 'z' is exist or not? How can I check this? I don't want to find index or anything else I just want to check if value zis exist or not.

假设我在变量 ex 中有一个字符串。var name="Stackoverflow". 我想检查这个字符串是否存在“z”?我该如何检查?我不想找到索引或其他任何东西,我只想检查值z是否存在。

Suppose with code. I have a variable.

假设有代码。我有一个变量。

var deleteboxvalue = "1111111111111111111111";
if(!deleteboxvalue.indexOf('z') >= 0){
alert("0 not exist");
return false;
}

回答by Sarfraz

You can use indexOflike this:

你可以这样使用indexOf

var name = "Stackoverflow"
var charExists = (name.indexOf('z') >= 0) ? true : false;
alert(charExists);

Or just (as pointed out by @Felix Kling):

或者只是(正如@Felix Kling 指出的那样):

var charExists = (name.indexOf('z') >= 0);