473,324 Members | 1,646 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Compare two strings .........

Hi.
I've got this simple snippets.
/*function isTheSame(string1,string2)
{
return (string1.toString() == string2.toString())
}*/
function isTheSame(string1,string2)
{
string1 = new RegExp(string1)
return string1.test(string2);
}
alert(isTheSame('pippo','pippo'));
Do you have any suggestion for improvement ? ;)
Take care.

Dec 10 '06 #1
3 10494
ASM
whisher a écrit :
Hi.
I've got this simple snippets.
/*function isTheSame(string1,string2)
{
return (string1.toString() == string2.toString())
}*/
function isTheSame(string1,string2)
{
string1 = new RegExp(string1)
return string1.test(string2);
}
alert(isTheSame('pippo','pippo'));
Do you have any suggestion for improvement ? ;)
Take care.
<script type="text/javascript">
function compare(s1, s2) { return s1===s2; }
pipo = 'popi';
</script>

<a href="#"
onclick="alert(compare(123, '123'));
return false;">test numbers ko</a>
<a href="#"
onclick="alert(compare(pipo, 'pipo'));
return false;">test variable ko</a>
<a href="#"
onclick="alert(compare('pipo', 'pipo'));
return false;">test strings ok</a>
<a href="#"
onclick="alert(compare(pipo, 'popi'));
return false;">test variable ok</a>
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date
Dec 10 '06 #2
whisher wrote:
function isTheSame(string1,string2)
{
string1 = new RegExp(string1)
return string1.test(string2);
}
alert(isTheSame('pippo','pippo'));
alert(isTheSame('pippo','ripippotorasaurus'));

Clearly they're not the same.
Mick
Dec 10 '06 #3
In comp.lang.javascript message <uT**************@twister.nyroc.rr.com>,
Sun, 10 Dec 2006 15:40:10, mick white <mi**@mickweb.comwrote:
>whisher wrote:
>function isTheSame(string1,string2)
{
string1 = new RegExp(string1)
return string1.test(string2);
}
alert(isTheSame('pippo','pippo'));

alert(isTheSame('pippo','ripippotorasaurus'));

Clearly they're not the same.
But

function isTheSame(string1, string2) {
return new RegExp(string1).test(string2) && new
RegExp(string2).test(string1)}

works better. Wouldn't use it myself, though, except to confuse.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
<URL:http://www.jibbering.com/faq/ Old RC FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: J.W. | last post by:
How do I compare strings in javascript? The "==" double equals or "!=" doesn't seem to work in this case. I'm sure string comparison has been explained before but searching Google didn't find...
3
by: Drew | last post by:
Hello - I am a converted VB programmer. What I am trying to do it compare two strings in an if statement. The problem is that when I use string.compare it always returns a negative 1. I have...
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
14
by: Samuel R. Neff | last post by:
Why would you cast two strings to objects to compare them? I saw code in an MS sample on MSDN and don't get it. if ( (object)name == (object)attr.name ) { both "name" and "attr.name" are...
5
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
11
by: balakrishnan.dinesh | last post by:
hi frnds, Im having two 20digit numbers, But while comparing those it is giiving wrong ouput in javascript. for example here is my code, my secanrio is , ~ If first 20 digit number is...
3
by: Twinkle | last post by:
HI there i want to compare between two strings char by char.every strings having a word document.first string name is strFileName and second string name is strFilename1. i want to compare...
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
2
by: Peter Proost | last post by:
Hi group, I want to compare path strings in order to sort them, assuming I have got: "a.txt" "dir1\c.txt" "e.txt" "dir1\d.txt" When I compare them using "e.text" would be greater than...
11
by: Tony | last post by:
Hello! Below I have two different ways to test if the instance tb.Text contains the string "Programmer" So which one to use is it just a matter of taste ? Or could it be some advantage to one...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.