473,603 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ 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(s tring1, "g");
str2 = string2.match(s tring1, "g");

if (str1 == str2) // STRING IS NOT the same :-( , what is wrong ???
{
app.alert("Stri ng are the same ");
}
else
{
app.alert("Stri ng 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 2409
"Ptaku25" <pg*********@o2 .pl> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.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(s tring1, "g");
str2 = string2.match(s tring1, "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(s tring1, "g");
str2 = string2.match(s tring1, "g");


Whatever it is you're trying to do, you're going about it the wrong way.
The String.prototyp e.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(s tring1, "g");
str2 = string2.match(s tring1, "g");
Whatever it is you're trying to do, you're going about it the wrong way.
The String.prototyp e.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.indexO f(...)' or `new RegExp("...").t est(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(s tring1, "g");
str2 = string2.match(s tring1, "g");


Whatever it is you're trying to do, you're going about it the wrong way.
The String.prototyp e.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(n ew 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(n ew 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("Stri ng 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.j s"></script>
....

you could simply write that

var Assertion = buRRRn.ASTUce.A ssertion;

var string1 = "Pablo has 3 cats and 1 dog";
var string2 = "Pablo has 3 cats";

try
{
Assertion.asser tEquals( string1, string2 );
}
catch( e )
{
trace( e );
}

and obtain that

## ComparisonFailu re : 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
4600
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 which also implements ==, <, <=, >, >=, etc. (Those operators are tested and working correctly.) If I assign map = "world", it saves the MyString's correctly in the map. But a subsequent call to map.find("hello") returns map.end(). Even more...
26
563
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 char". How can we compare two different objects for equality ? Is some conversion is being done before that comparison ?
3
1903
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 am not able to get the seconds value (30/60=0.5) So can anyone give me ideas, throught which i can do similar coding in AWK programming. Secondly i am comparing two number. Ex: 91402777777 is the dialled number and the area code is 91 (Which
37
2777
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 behaviour, which is returning a value which is, really, id({}) < id(). He also said that default equality comparison will continue to be identity-based. This means that x == y will never raise an exception, as is the situation is now. Here's his reason:
12
1539
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 have any suggestions? My code is below. Thanks in Advanced, Elena Option Compare Binary
7
2764
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, here is my table structure, Name: Table1
3
2945
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 OKAY TO ADD SOMETHING. IF IT'S AFTER 9:30 TODAY, YOU MUST ADD IT TOMORROW. CODE: nowtime=now() deadlinetime=formatdatetime(now(),2) + " 8:30:00 AM" response.write "NOW: " & nowtime & "<BR>" response.write "Deadline: " & deadlinetime & "<BR>"
1
6311
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 a software buffer of size 3MB. This is needed so as to see which function to use to read from the file. As the files used range from very large (>30GB) to very small (<3MB), I have enabled large file support and I obtain the file size by using the...
28
5774
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) {
0
7928
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8415
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8405
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8060
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
3903
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1514
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.