By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,720 Members | 2,145 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,720 IT Pros & Developers. It's quick & easy.

Comparison of string is not working properly :-(

P: n/a
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 ???

Jan 12 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Ptaku25" <pg*********@o2.pl> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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.
Jan 12 '06 #2

P: n/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?

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Jan 12 '06 #3

P: n/a

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.


Jan 12 '06 #4

P: n/a
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
Jan 12 '06 #5

P: n/a
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
Jan 13 '06 #6

P: n/a
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/AS...avaScript.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-...isonFailure.es

HTH
zwetan
Jan 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.