Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

String compare

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Muhs - 12 Mar 2008 17:20 GMT
Hi !!
i want to compare two strings and return the difference

For example, i have two strings,
string str1="then";
string str2="than";

it compared the two strings and returns 1, as only one character is
different (a and e).
Is there a function in c# that can do this??
Jon Skeet [C# MVP] - 12 Mar 2008 17:29 GMT
> i want to compare two strings and return the difference
>
[quoted text clipped - 5 lines]
> different (a and e).
> Is there a function in c# that can do this??

I don't think there's anything built into .NET to do that. It's a
relatively tricky business. For instance, your example was simple - but
what about:

"Foo is good"
"Food is good"

*We* can see that you just need to add the "d" to the first line, but a
character-by-character analysis would show everything after "Foo" as
being different.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

rossum - 12 Mar 2008 17:36 GMT
>Hi !!
>i want to compare two strings and return the difference
[quoted text clipped - 6 lines]
>different (a and e).
>Is there a function in c# that can do this??
Have a look at the Levenshtein Distance measure,
http://en.wikipedia.org/wiki/Levenshtein_distance which may be what
you are looking for.  As far as I am aware it is not in .NET, though
it is possible there is a C# version of the algorithm out there
somewhere.

rossum
Martin Bonner - 12 Mar 2008 17:43 GMT
> Hi !!
> i want to compare two strings and return the difference
[quoted text clipped - 6 lines]
> different (a and e).
> Is there a function in c# that can do this??

I don't think so.

This is quite easy, so I suspect it is homework.  You won't learn if
you get somebody else to write the code for you.  If you have written
some code and are having trouble, then post it with a description of
the problem and we can probably help.

If it is not homework, then what is the application?  (I can think of
a number of issues with the specification as it stands, and the
resolution will depend on the application.)
Muhs - 12 Mar 2008 18:06 GMT
lol.. i know what u mean by being it a homework...  its not exactly a
homework..
its my degree project which i'll be working on till september... and
ive just stated.. My project is about mutation clustering...the data
that i have is in the form of 1's and 0's
(10001010101010101010000011... could be int or string).  And i have to
cluster them based on the hamming distance...
I was just thinking of ways to compare and cluster the data... (i
thought i could compare the each string and getthe number of different
bits)
or i can XOR each int and find the distance...
somebody in the reply above sent a link... and i found this hamming
distance algo:

int dist = 0;
           int val = 0;
           int x= 8;
           int y=15;
           val = x ^ y;  // this will be equal to 7

             while(val!=0)
             {
               ++dist;
               val = val - 1;
             }

but this

> > Hi !!
> > i want to compare two strings and return the difference
[quoted text clipped - 17 lines]
> a number of issues with the specification as it stands, and the
> resolution will depend on the application.)
Muhs - 12 Mar 2008 18:08 GMT
int distance = 0;
           int val = 0;
           int x= 8;
           int y=15;
           val = x ^ y;

             while(val!=0)
             {
               ++distance;
               val = val - 1;
             }

but the distance that i get is 7, same as val... i was thinking of
converting 7 to binary and count the number of 1's (111) that way i
could get distance 3..

> > Hi !!
> > i want to compare two strings and return the difference
[quoted text clipped - 17 lines]
> a number of issues with the specification as it stands, and the
> resolution will depend on the application.)
Muhs - 12 Mar 2008 18:13 GMT
lol.. i know what you mean by being it a homework... actually its my
final year degree project.. on which i'll be working on till
sepetember... my project is about mutation clustering... and i have
data in the form of 1's and 0's(data= 101010000011000001, could be int
or string)... now i want to cluster the data based on some similarity
measure.... i thought i could compare the string data and find the
number of characters that are different... but somebody above sent a
link and there was this algo of hamming distance... i wrote  the algo
as follows:

         int distance = 0;
           int val = 0;
           int x= 8;
           int y=15;
           val = x ^ y;

             while(val!=0)
             {
               ++distance;
               val = val - 1;
             }

but the distance that i get is 7, same as val... i was thinking of
converting 7 to binary and count the number of 1's (111) that way i
could get distance 3..

> > Hi !!
> > i want to compare two strings and return the difference
[quoted text clipped - 17 lines]
> a number of issues with the specification as it stands, and the
> resolution will depend on the application.)
Martin Bonner - 13 Mar 2008 16:42 GMT
> lol.. i know what you mean by being it a homework... actually its my
> final year degree project.. on which i'll be working on till
[quoted text clipped - 20 lines]
> converting 7 to binary and count the number of 1's (111) that way i
> could get distance 3..

Well yes.  As currently coded, the while loop is equivalent to:
    distance = val;
    val = 0;
(... provided x,y are constrainted to be >= 0).  Go look at the
wikipedia page again, and code the updated correctly, and it will
indeed count the bits.

> > > Hi !!
> > > i want to compare two strings and return the difference
[quoted text clipped - 17 lines]
> > a number of issues with the specification as it stands, and the
> > resolution will depend on the application.)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.