PDA

View Full Version : how to do string check in javascript .....



2009old
12-29-2009, 04:37 PM
string check in javascript


do suggest me something ....... :confused:

if you have any two words like , "me" and "you".

abduraooft
12-29-2009, 04:48 PM
What do you mean by "string check"?

var foo="me";
var bar="you";
if(foo==bar)
alert('equal');
else
alert("not equal");

2009old
12-29-2009, 05:09 PM
oh thanks i got it .. I mean

var foo="me";

if(foo=="me" || foo=="you" )
alert('inIn 2010');
else
alert("outOfParty");

:) thanks

2009old
12-29-2009, 07:02 PM
but why this is not working



if(foo !="me" || foo !="you" )
alert("outOfParty");

any idea


thanks...

Old Pedant
12-29-2009, 09:37 PM
*THINK* about it for a moment.



var foo = "me";

if ( foo != "me" ) alert( "foo is NOT me");
if ( foo != "you" ) alert( "foo is NOT you");

No surprises, right? Only the second alert shows up.



var foo = "me";

var isNotMe = ( foo != "me");
var isNotYou = ( foo != "you");

if ( isNotMe ) alert( "foo is not me");
if ( isNotYou ) alert( "foo is not you");

You see? I converted the "conditions" (the != comparisons) to boolean values and put them in variables.

Then I based my alert's on the boolean variables.

Still no surprises, right? Only the second alert shows up. What you expect, right?

Okay...


var foo = "me";

var isNotMe = ( foo != "me");
var isNotYou = ( foo != "you");

var eitherOneIsNot = isNotMe || isNotYou;
// isNotMe is false, but isNotYou is true, so eitherOneIsNot is true, right?

if ( eitherOneIsNot ) alert( "either one is not");
// so of course the alert is displayed


See?

I *assume* that what you *meant* to code was this:


var foo = "me";

if ( foo != "me" && foo != "you" ) alert( "foo is NEITHER me nor you");


Alternatively, and perhaps easiert to understand:


var foo = "me";

if ( ! ( foo == "me" || foo == "you" ) ) alert( "It is NOT true that foo is either me or you");

2009old
12-29-2009, 10:31 PM
ya .. you r right ......


U did good analysis ...

it is simple concept
1 AND 1 ->1
1 AND 0 ->0
0 AND 1 ->0
0 AND 0 ->0

1 OR 1 ->0
1 OR 0 ->0
0 OR 1 ->0
0 OR 0 ->1

which I was forgot at the time I posted 2nd time ..... :)

keep in mind this concept and do all ...

thanks
welcome 2010 bye-2 2009

Old Pedant
12-29-2009, 10:34 PM
it is simple concept
1 AND 1 ->1
1 AND 0 ->0
0 AND 1 ->0
0 AND 0 ->0

1 OR 1 ->0
1 OR 0 ->0
0 OR 1 ->0
0 OR 0 ->1


No. ALL your OR values are wrong.

Should be


1 OR 1 ->1
1 OR 0 ->1
0 OR 1 ->1
0 OR 0 ->0

2009old
12-29-2009, 11:02 PM
that is what I want that you did ...

that is why I was doing error .. i correct it

thanks for participate

thanks