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

Discussion in 'Javascript' started by whisher, Dec 10, 2006.

  1. whisher

    whisher Guest

    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.
     
    whisher, Dec 10, 2006
    #1
    1. Advertising

  2. whisher

    ASM Guest

    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
     
    ASM, Dec 10, 2006
    #2
    1. Advertising

  3. whisher

    mick white Guest

    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
     
    mick white, Dec 10, 2006
    #3
  4. In comp.lang.javascript message <uTVeh.127$>,
    Sun, 10 Dec 2006 15:40:10, mick white <> wrote:
    >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.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Dec 10, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GenxLogic
    Replies:
    3
    Views:
    1,289
    andrewmcdonagh
    Dec 6, 2006
  2. Neroku
    Replies:
    12
    Views:
    559
    Oliver Wong
    Feb 12, 2007
  3. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
  4. Replies:
    9
    Views:
    563
    Stefan Ram
    Jul 29, 2008
  5. Peter

    how do you compare two strings

    Peter, Sep 24, 2003, in forum: ASP General
    Replies:
    1
    Views:
    182
    Ray at
    Sep 24, 2003
Loading...

Share This Page