473,468 Members | 4,558 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Comparison of string is not working properly :-(

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
6 2401
"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
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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
26
by: junky_fellow | last post by:
Consider the following piece of code: char *str = "Hello"; if (str = "Hello") printf("\nstring matches\n"); str is pointer to char and "Hello" is a string literal whose type is "array of...
3
by: Chandu | last post by:
Hi, I am working on awk programming which is similar to C programming and have got a doubt about time function returning a float value. Ex: 01:01:30 should return 61.5 when i have tried my way i...
37
by: spam.noam | last post by:
Hello, Guido has decided, in python-dev, that in Py3K the id-based order comparisons will be dropped. This means that, for example, "{} < " will raise a TypeError instead of the current...
12
by: Elena | last post by:
I'm trying to compare two pieces of text. If the cases are different, I want it to be the same as if the text were different. I tried doing a binary compare, but it's not working. Does anyone...
7
by: mr.nimz | last post by:
hello, this is antenio. recently i've come to a problem. i got a way through it, somehow, still it left me in a curious state, so i'm posting it here, if i can get an answer from some techy, ...
3
by: Joey Martin | last post by:
Please look at code below. I am trying to compare the NOW time with a "deadline" time. Please help. Just not sure why this is not working. I need to be able to say IF IT'S BEFORE 9:30 TODAY, IT'S...
1
by: Lars B | last post by:
Hey guys, I have written a C++ program that passes data from a file to an FPGA board and back again using software and DMA buffers. In my program I need to compare the size of a given file against...
28
by: kaferro | last post by:
What is the safest way to make an argv comparison? The code below works. #include <iostream> #include <string> using namespace std; int main(int argc, char *argv) {
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.