6 Replies Latest reply: May 13, 2007 8:25 PM by 807606

# how to consider 2 string as equals if they are at least 80% alike?

Hi all,

say I have the following strings
``````String str1 ="  -QNYNJ24706"
String str2 = " ^QNYNJ2470"``````
Obviously, the 2 strings are NOT equal, but they are alike, there are only 3 things that distinguish between str1 and str2

1. space
2. - vs ^
3. 6 is missing in str2

My question is this: how can I write a method that if the str1 is at least 80% equals to str2 than consider str1 equals str2

(spaces are irrelevant by trim, so I'm left with only 2 different characters, 2 out of 10 chars from str2 is 20%)

In other words, I'm trying to add a tolerance to "equals"

hope you got the idea. thanks for any pointers!
• ###### 1. Re: how to consider 2 string as equals if they are at least 80% alike?
There are several ways that this could be done, and most depends on what exactly you mean when you say "close enough". This is one possibility:
``````class Fubar
{

private static boolean closeEnough(String str1, String str2)
{
int matches = 0;

str1 = str1.trim();
str2 = str2.trim();

for (int i = 0; i < str1.length(); i++)
{
if (i < str2.length())
{
if (str1.charAt(i) == str2.charAt(i))
{
matches++;
}
}
}
if ((double) matches / str1.length() > 0.8)
{
return true;
}
else
{
return false;
}
}

public static void main(String[] args)
{

String str1 = "  -QNYNJ24706";
String str2 = " ^QNYNJ2470";
System.out.println("closeEnough(str1, str2) = " + closeEnough(str1, str2));

}
}``````
• ###### 2. Re: how to consider 2 string as equals if they are at least 80% alike?
Depends how you define "80% alike".

For instance, are the following more than 80% alike?
``````thorough
through``````
• ###### 3. Re: how to consider 2 string as equals if they are at least 80% alike?
thank you petes1234

this is exactly what I had in mind!!!

Jverd - you are right with your example but it's not a consideration in my application, thanks for the insight!

I added a tolerance to the function:

Thanks again petes1234
``````class Fubar
{
private static boolean closeEnough(String str1, String str2, double tolerance)
{
int matches = 0;

str1 = str1.trim();
str2 = str2.trim();

for (int i = 0; i < str1.length(); i++)
{
if (i < str2.length())
{
if (str1.charAt(i) == str2.charAt(i))
{
matches++;
}
}
}
if ((double) matches / str1.length() >= tolerance)
{
return true;
}
else
{
return false;
}
}

public static void main(String[] args)
{

String str1 = "  -QNYNJ24706";
String str2 = " ^QNYNJ2470";
double tolerance = 0.81;
System.out.println("closeEnough(str1, str2) = " + closeEnough(str1, str2, tolerance )+ " with tolerance="+tolerance);

}
}``````
result:
closeEnough(str1, str2) = true with tolerance=0.81
• ###### 4. Re: how to consider 2 string as equals if they are at least 80% alike?
Keep in mind that his method ignores the space in front as a difference.
• ###### 5. Re: how to consider 2 string as equals if they are at least 80% alike?
yes, I know. thank you
• ###### 6. Re: how to consider 2 string as equals if they are at least 80% alike?
thank you petes1234
You're welcome.
I added a tolerance to the function:
A good idea.

Obviously this demonstrates a functional and slippery definition only and would have to be commented heavily in placed in any class.