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 ");
    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. Advertisements

  2. Ptaku25

    McKirahan Guest


    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
    1. Advertisements

  3. 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?


    Michael Winter, Jan 12, 2006
  4. Ptaku25

    Ptaku25 Guest

    Michael Winter napisal(a):

    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???

    Ptaku25, Jan 12, 2006
  5. Use `string1.indexOf(...)' or `new RegExp("...").test(string1)' or
    and learn to quote.

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

    RobG Guest

    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.
    RobG, Jan 13, 2006
  7. Ptaku25

    zwetan Guest

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

    <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";

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

    and obtain that

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

    you can find more info here:

    and a basic tutorial here for unit testing your code:

    if you re only interested in the code comparing 2 strings for their
    difference look the source code here
    and adapt to your need

    zwetan, Jan 13, 2006
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.