Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check variable if char exist or not [duplicate]

Tags:

javascript

Possible Duplicate:
How to tell if a string contains a certain character in 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 z is exist or not.

Suppose with code. I have a variable.

var deleteboxvalue = "1111111111111111111111";
if(!deleteboxvalue.indexOf('z') >= 0){
alert("0 not exist");
return false;
}
like image 928
Rahul Singh Avatar asked Aug 17 '26 12:08

Rahul Singh


1 Answers

You can use indexOf like this:

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

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

var charExists = (name.indexOf('z') >= 0);
like image 189
Sarfraz Avatar answered Aug 19 '26 01:08

Sarfraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!