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
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
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
Thanks
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["c name"]%>";
if (variable1=="Tom & Jerry")
{
document.write('first');
}
else if (variable1=="Tom")
{
document.write('second');
}
else if (variable1=="Jerry")
{
document.write('Jerry');
}
</script>
<script language="JAVASCRIPT" type="TEXT/JAVASCRIPT">
var variable1= "<%=Request.QueryString["c
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-
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
Check the case (upper case and lower case aren't same)
Check for any spaces after/before the string
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also make sure that the variable1="Tom & Jerry" doesn't reed "Tom & Jerry"
-r-
-r-
var variable1= "<%=Request.QueryString["c name"]%>";
alert(variable1);
if (variable1=="Tom & Jerry")
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["c name"]%>";
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.
<script language="JAVASCRIPT" type="TEXT/JAVASCRIPT">
var variable1= "<%=Request.QueryString["c
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.Query String["cn ame"]%>");
Tom%20&%20Jerry
you can fix that by using unescape()
variable1 = unescape("<%=Request.Query
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!
- 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!
ASKER
sorry - i meant to refer to mshogren's solution
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
Don't forget to put the link for this question (http:Q_21679712.html):
https://www.experts-exchange.com/Community_Support/askQuestion.jsp
ASKER
sorry after tking my time and readin logically down through the solutions - mshogren - did suggest the alert() first. Sorry
OK no problem :)
-r-