Ampersand in string comparison

Hi

I have a string comparison in an if statement but the ampersan (&) is causing a problem

if (variable1=="Tom & jerry")......

The if statement simply doesn't work - its as if variable1 is not "Tom & jerry" - BUT IT IS!!!! How do I overcome this?

Thanks
bowemcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RoonaanCommented:
Please show more code. What you state sounds very obvious indeed that both variable1 and tom & jerry should match, but as they don't there should be some difference you overlooked.

-r-
bowemcAuthor Commented:
i will get the code block - but while i get it - i was wandering is & being detected as an operator of some sort? I know in some language if you want to display "/" you must enter "//" as otherwise "/" is thought to be an escape character? is it anything like this?

Thanks
bowemcAuthor Commented:
Below is the code: the second and thrid branches execute as desired - i'm sure it has to do with the &

<script language="JAVASCRIPT" type="TEXT/JAVASCRIPT">
var variable1= "<%=Request.QueryString["cname"]%>";
if (variable1=="Tom & Jerry")
 {
       document.write('first');
 }
else if (variable1=="Tom")
 {
       document.write('second');
 }
else if (variable1=="Jerry")
 {
       document.write('Jerry');
 }
</script>
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

RoonaanCommented:
No, inside strings the only thing you need to escape would be the quote you use to surround the string:
a) when using double quotes ("string") you need to escape any quotes: "string with \" in it";
b) when using single quotes( 'string') you need to escape any single quotes: 'string with \' single quote';

You can add other escape characters like \r, \n and \t to add newlines/tabs, and use \0, \1, \2, to add specific ascii characters.

-r-
Harisha M GEngineerCommented:
bowemc,

Check the case (upper case and lower case aren't same)
Check for any spaces after/before the string
mshogrenCommented:
I suspect there may be a problem because you are getting the value from the querystring, where & has a special meaning.  Check you are getting the value you think by inserting

alert(variable1)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RoonaanCommented:
Also make sure that the variable1="Tom & Jerry" doesn't reed "Tom &amp; Jerry"

-r-
Harisha M GEngineerCommented:
var variable1= "<%=Request.QueryString["cname"]%>";

alert(variable1);

if (variable1=="Tom & Jerry")
jessegivyDeveloperCommented:
Try alerting your variable to ensure what mgh_mgharish is talking about, it has to be an exact match, it's a string comparison based on ascii values.  Put pipe before and after to check like this:

<script language="JAVASCRIPT" type="TEXT/JAVASCRIPT">
var variable1= "<%=Request.QueryString["cname"]%>";
alert("|"+variable1+"|");
if (variable1=="Tom & Jerry")
 {
       document.write('first');
 }
else if (variable1=="Tom")
 {
       document.write('second');
 }
else if (variable1=="Jerry")
 {
       document.write('Jerry');
 }
</script>

Your syntax is correct so it's gotta be a prob with the data.  HTML and URL's have an issue with the ampersand as text, javascript doesn't.
mshogrenCommented:
In fact if you are getting something from the querystring with spaces in it it probably comes out as

Tom%20&%20Jerry

you can fix that by using unescape()

variable1 = unescape("<%=Request.QueryString["cname"]%>");
bowemcAuthor Commented:
hi

- mgh_mgharish your alert solution worked - for some reason when "Tom & Jerry" was brought in it was only brought in as far as the & - heance it was just "Tom "

Thanks for resolving this for me!

bowemcAuthor Commented:
sorry - i meant to refer to mshogren's solution
bowemcAuthor Commented:
no - i was right the first time! Really - i'm sorry! I'm all afluster!
Harisha M GEngineerCommented:
bowemc, open a zero point question in CS to open this thread and change the answer
Don't forget to put the link for this question (http:Q_21679712.html):

http://www.experts-exchange.com/Community_Support/askQuestion.jsp
bowemcAuthor Commented:
sorry after tking my time and readin logically down through the solutions - mshogren - did suggest the alert() first. Sorry
Harisha M GEngineerCommented:
OK no problem :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.