This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Comparision of Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Comparision of Strings" Watch "Comparision of Strings" New topic
Author

Comparision of Strings

ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
Hi

Like i have two Strings:


String 1: My name is abc

String 2: My name abc


If we compare through java then these two above Strings are different but when we see normally then it appears to be same.

Can we do such things in java which check the String intelligently.Means if we companre above two Strings then it give result as MATCHED inspite on Not Matched

Thanks
Ram
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12909
    
    3

That is very difficult, because what exactly means "appears to be the same"? They appear to be the same when a human quickly looks at both strings?

In order to write a program you must be able to exactly specify what this means, which is very difficult. "Appears to be the same" is a very fuzzy and inexact definition. Note that it might also be different for different people - maybe for you those two strings appear to be the same if you look at them quickly, but if I look at them quickly then I see immediately that they are different.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
One idea would be to use some kind of diff algorithm and count the number of differences.

It's hard to give more advice without knowing what you want to do this for.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
ram gaurav
Ranch Hand

Joined: Mar 29, 2006
Posts: 208
Thanks for replies.

Like i have two Strings :

String 1: My name is abc

String 2: My name abc

These above two Strings are different but i want that when this type of copmparision will come it will show result as equal inspite of not equal
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12909
    
    3

Ok, we understood that, you're just repeating your question.

Can you please explain exactly when you want the comparison to return "equal" and when it is "not equal"? I mean exactly, without fuzzy descriptions like "when they look almost the same".
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Or at least start by showing more examples.

For example, what about

My name is

My abc

My name is cba

My name is Ulf

My name is Ilja

Your name is abc

Name is abc

My is abc

Which of those should "be equal to" String 1?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35237
    
    7
You might be talking about the concept of using a stopword filter for text. The idea is to filter out common words like "the", "and" and "is" before comparing text, because they don't contribute to the meaning of the text.

A related important concept is stemming, which reduces words like "fishing", "fished", "fish", and "fisher" to their root, "fish".

Wikipedia has more to say on both concepts: stop words Stemming

Apache Lucene is an example of a Java library that uses both these techniques to compare texts.


Android appsImageJ pluginsJava web charts
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 408
There is no such method which compares strings so intelligently as you want.So,you have to make your own method and use that method wherever you want.
I think your requirement is only for small sentences like :
String1 = My name is abc
String2 = My name abc
You can go towards grammer side also like..
Adjective+noun+adverb+noun,so that wherever you get this sequence you can replace it with Adjective+noun+noun.
I don't know to which level you want,means can the sentences be big also...
I have told you option ,rest you can try and decide yourself
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Sure it's easy:



Levenshtein Distance
[ February 09, 2007: Message edited by: Tony Morris ]

Tony Morris
Java Q&A (FAQ, Trivia)
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12909
    
    3

Well Tony, you wouldn't even have to think very hard to implement it in Java, because you can just copy and paste the source code from the website you linked to!
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Jesper Young:
Well Tony, you wouldn't even have to think very hard to implement it in Java, because you can just copy and paste the source code from the website you linked to!


Nice one
I have written in Java before (using destructive update on an array), but I also have it written in Scala (which is callable from Java) using pure function calls (the Scala compiler tail call eliminates). I have also written it in C# and Haskell. LCS-based delta (and therefore, ED) is a problem that I happen to be very familiar with
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Comparision of Strings
 
Similar Threads
regular expression problem
String confusion
Query about String ??
String equals
Urgent String question