Comparison of string is not working properly :-(

Discussion in 'Javascript' started by Ptaku25, Jan 12, 2006.

  1. Ptaku25

    Ptaku25 Guest

    Hi all

    // my code
    var string1= "Pablo has 3 cats and 1 dog";
    var string2= "Pablo has 3 cats";
    var str1 = "";
    var str2 = "";


    str1 = string1.match(string1, "g");
    str2 = string2.match(string1, "g");

    if (str1 == str2) // STRING IS NOT the same :-( , what is wrong ???
    {
    app.alert("String are the same ");
    }
    else
    {
    app.alert("String is different...");
    }

    I want to compare two strings in 'if' statement, which are asssigned to
    variables, If I compare two variable which have assigned the same (in
    my opinion) string after RegExp match function the string is different
    unfortunatellly :-(

    but when I compare in 'if' statement:
    if( str1 == "Pablo has 3 cats") or
    if( str2 == "Pablo has 3 cats") or
    if("Pablo has 3 cats" == str2) or
    if("Pablo has 3 cats" == str2)
    THIS COMPARISON IS WORKING WELL and strings is the same :)


    I wonder why my string is not the same ???
    the match function returned me the same string for str1 and str2
    variable when I invoke alert funtion on it, but for 'if' statement the
    string is not the same :-(, what is wrong ???
    Ptaku25, Jan 12, 2006
    #1
    1. Advertising

  2. Ptaku25

    McKirahan Guest

    "Ptaku25" <> wrote in message
    news:...
    > Hi all
    >
    > // my code
    > var string1= "Pablo has 3 cats and 1 dog";
    > var string2= "Pablo has 3 cats";
    > var str1 = "";
    > var str2 = "";
    >
    >
    > str1 = string1.match(string1, "g");
    > str2 = string2.match(string1, "g");


    [snip]

    Why are you using ".match()"?

    match Method :

    Returns, as an array, the results of a search on a string using a supplied
    Regular Expression object.
    McKirahan, Jan 12, 2006
    #2
    1. Advertising

  3. On 12/01/2006 15:25, Ptaku25 wrote:

    > // my code
    > var string1= "Pablo has 3 cats and 1 dog";
    > var string2= "Pablo has 3 cats";
    > var str1 = "";
    > var str2 = "";
    >
    > str1 = string1.match(string1, "g");
    > str2 = string2.match(string1, "g");


    Whatever it is you're trying to do, you're going about it the wrong way.
    The String.prototype.match method takes only one argument, and that
    argument is a regular expression.

    Start by explaining /exactly/ what you're trying to achieve. You mention
    strings being the 'same', but string1 and string2 clearly aren't. Are
    you trying to identify substrings?

    [snip]

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, Jan 12, 2006
    #3
  4. Ptaku25

    Ptaku25 Guest

    Michael Winter napisal(a):
    > On 12/01/2006 15:25, Ptaku25 wrote:
    >
    > > // my code
    > > var string1= "Pablo has 3 cats and 1 dog";
    > > var string2= "Pablo has 3 cats";
    > > var str1 = "";
    > > var str2 = "";
    > >
    > > str1 = string1.match(string1, "g");
    > > str2 = string2.match(string1, "g");

    >
    > Whatever it is you're trying to do, you're going about it the wrong way.
    > The String.prototype.match method takes only one argument, and that
    > argument is a regular expression.
    >
    > Start by explaining /exactly/ what you're trying to achieve. You mention
    > strings being the 'same', but string1 and string2 clearly aren't. Are
    > you trying to identify substrings?



    Yes I would like to identify substring!,
    and as I underdstand, I going to wrong way using match() method???

    Which method or tricks I should use to make identyfication some
    substring in any string???





    >
    > [snip]
    >
    > Mike
    >
    > --
    > Michael Winter
    > Prefix subject with [News] before replying by e-mail.
    Ptaku25, Jan 12, 2006
    #4
  5. Ptaku25 wrote:

    > [...]
    > Which method or tricks I should use to make identyfication some
    > substring in any string???


    Use `string1.indexOf(...)' or `new RegExp("...").test(string1)' or
    `/.../.test(string1)'

    > [...]


    and learn to quote.


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 12, 2006
    #5
  6. Ptaku25

    RobG Guest

    Ptaku25 wrote:
    > Michael Winter napisal(a):
    >
    >>On 12/01/2006 15:25, Ptaku25 wrote:
    >>
    >>
    >>>// my code
    >>>var string1= "Pablo has 3 cats and 1 dog";
    >>>var string2= "Pablo has 3 cats";
    >>>var str1 = "";
    >>>var str2 = "";
    >>>
    >>>str1 = string1.match(string1, "g");
    >>>str2 = string2.match(string1, "g");

    >>
    >>Whatever it is you're trying to do, you're going about it the wrong way.
    >>The String.prototype.match method takes only one argument, and that
    >>argument is a regular expression.
    >>
    >>Start by explaining /exactly/ what you're trying to achieve. You mention
    >>strings being the 'same', but string1 and string2 clearly aren't. Are
    >>you trying to identify substrings?

    >
    >
    >
    > Yes I would like to identify substring!,
    > and as I underdstand, I going to wrong way using match() method???
    >
    > Which method or tricks I should use to make identyfication some
    > substring in any string???
    >


    var string1= "Pablo has 3 cats and 1 dog";
    var string2= "Pablo has 3 cats";


    See if string2 is anywhere in string1:

    var x = new RegExp(string2).test(string1);
    // x is a boolean with value 'true'


    Get all instances of string2 in string1:

    var x = string1.match(new RegExp(string2, 'g'));
    // x is an array of length 1 and value: ['Pablo has 3 cats']


    Get all instances of 'a' followed by a character in string1:

    var x = string1.match(new RegExp('a.','g'));
    // x is an array, length 4, value: ['ab', 'as', 'at', 'an']


    Regular expressions are almost a language of their own.


    --
    Rob
    RobG, Jan 13, 2006
    #6
  7. Ptaku25

    zwetan Guest

    Hi,

    >
    > // my code
    > var string1= "Pablo has 3 cats and 1 dog";
    > var string2= "Pablo has 3 cats";

    [snip]
    > else
    > {
    > app.alert("String is different...");
    > }
    >
    > I want to compare two strings in 'if' statement, which are asssigned to
    > variables, If I compare two variable which have assigned the same (in
    > my opinion) string after RegExp match function the string is different
    > unfortunatellly :-(
    >


    humm you could use ASTUce framework Assertions methods to
    test the equality of your strings

    using
    <script type="text/javascript" src="lib/core2_v1.0.1_JS.js"></script>
    <script type="text/javascript" src="lib/ASTUce_v1.0.0.js"></script>
    ....

    you could simply write that

    var Assertion = buRRRn.ASTUce.Assertion;

    var string1 = "Pablo has 3 cats and 1 dog";
    var string2 = "Pablo has 3 cats";

    try
    {
    Assertion.assertEquals( string1, string2 );
    }
    catch( e )
    {
    trace( e );
    }

    and obtain that

    ## ComparisonFailure : expected:<... and 1 dog> but was:<...> ##


    you can find more info here:
    http://www.burrrn.com/projects/ASTUce.html

    and a basic tutorial here for unit testing your code:
    http://www.zwetan.com/blog/buRRRn/ASTUce/Hands_on_with_ASTUce_in_JavaScript.html

    if you re only interested in the code comparing 2 strings for their
    difference look the source code here
    and adapt to your need
    http://live.burrrn.com/browser/ECMA-262/ASTUce/trunk/src/buRRRn/ASTUce/ComparisonFailure.es

    HTH
    zwetan
    zwetan, Jan 13, 2006
    #7
    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. Aaron Powell
    Replies:
    0
    Views:
    1,196
    Aaron Powell
    Jul 24, 2003
  2. Jasper
    Replies:
    0
    Views:
    487
    Jasper
    Jun 28, 2004
  3. MSFT
    Replies:
    11
    Views:
    593
    Grimsa
    Dec 3, 2003
  4. Jeff
    Replies:
    1
    Views:
    10,288
    MrCeri
    Dec 7, 2007
  5. Deepu
    Replies:
    1
    Views:
    236
    ccc31807
    Feb 7, 2011
Loading...

Share This Page