Comparison of string is not working properly :-(

P

Ptaku25

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

McKirahan

Ptaku25 said:
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.
 
M

Michael Winter

// 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
 
P

Ptaku25

Michael Winter napisal(a):
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
 
T

Thomas 'PointedEars' Lahn

Ptaku25 said:
[...]
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
 
R

RobG

Ptaku25 said:
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???

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.
 
Z

zwetan

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top