Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Comparison of string is not working properly :-(

Reply
Thread Tools

Comparison of string is not working properly :-(

 
 
Ptaku25
Guest
Posts: n/a
 
      01-12-2006
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 ???

 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      01-12-2006
"Ptaku25" <> wrote in message
news: 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.


 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      01-12-2006
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.
 
Reply With Quote
 
Ptaku25
Guest
Posts: n/a
 
      01-12-2006

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.


 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-12-2006
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
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      01-13-2006
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
 
Reply With Quote
 
zwetan
Guest
Posts: n/a
 
      01-13-2006
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Yahoo group not working properly with Firefox History Fan Firefox 3 06-27-2005 02:30 AM
ICS not working properly AuthorizedUser Wireless Networking 2 08-23-2004 04:46 AM
More American Graffiti: Properly Framed, Properly Scored? Scot Gardner DVD Video 0 09-02-2003 02:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57