Link to home
Start Free TrialLog in
Avatar of bowemc
bowemc

asked on

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
Avatar of Roonaan
Roonaan
Flag of Netherlands image

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-
Avatar of bowemc
bowemc

ASKER

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
Avatar of bowemc

ASKER

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>
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-
bowemc,

Check the case (upper case and lower case aren't same)
Check for any spaces after/before the string
ASKER CERTIFIED SOLUTION
Avatar of mshogren
mshogren

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also make sure that the variable1="Tom & Jerry" doesn't reed "Tom &amp; Jerry"

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

alert(variable1);

if (variable1=="Tom & Jerry")
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.
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"]%>");
Avatar of bowemc

ASKER

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!

Avatar of bowemc

ASKER

sorry - i meant to refer to mshogren's solution
Avatar of bowemc

ASKER

no - i was right the first time! Really - i'm sorry! I'm all afluster!
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):

https://www.experts-exchange.com/Community_Support/askQuestion.jsp
Avatar of bowemc

ASKER

sorry after tking my time and readin logically down through the solutions - mshogren - did suggest the alert() first. Sorry
OK no problem :)