当更多字符看起来像时,在 equal() 中获得 true
Getting true in equal() when more characters look like
当大多数字符相似时如何识别两个字符串
我想在这个示例中得到 true
"Hello Wolrld" == "HelloWorld"
或
"hello world!!" == "helloworld"
我知道这些不相等,但是由于大部分字符相同,所以对我来说足够了
提前致谢
您可以计算 Levenshtein distance of the two strings (see for example this C# implementation),然后定义一个阈值,您认为达到该阈值的字符串为 "equal"。
什么是合理的阈值取决于您的要求。可能,将谓词定义为 d <= a * Math.Min(string1.Length, string2.Length)
应该可行,其中 d
是字符串的 Levenshtein 距离,而 a
是 "similarity" 在 0 和 1 之间的一个因子。在您的示例中a==0.3
应该可以。
使用这个
Regex.Replace(textBox1.Text, @"[^0-9a-zA-Z]+", "").ToLower() == your string in lower case
如果您正在寻找非常基本的检查,您可以使用 Zip
枚举字符以比较它们,计算匹配的字母,如果匹配的数量超过特定阈值则报告 true .如果一个字符串是另一个字符串的移位版本,则不会捕获它;它只会在同一索引处捕获共同的字母。
public static class ExtensionMethods
{
public static bool FuzzyCompare(this string lhs, string rhs, float ratioRequired)
{
var matchingLetters = lhs.Zip
(
rhs,
(a,b) => a == b ? 1 : 0
)
.Sum();
return (float)matchingLetters / (float)lhs.Length > ratioRequired;
}
}
要比较两个字符串以查看它们是否匹配至少一半的字母,请传递 0.5 的 ratioRequired
。
public static void Main()
{
var a = "ABCD";
var b = "ABCDEFGHI";
Console.WriteLine( a.FuzzyCompare(b, 0.5F) );
}
输出:
True
如果 80% 的词比较相似,则为真试试这个
String str1 = "Hello world";
String str2 = "Helloworld!!";
char[] charArray;
int per = 0;
int c = 0;
if (str1.length() > str2.length()) {
per = (str1.length() * 80) / 100; // 80% per match logic
charArray = str1.toCharArray();
for (int i = 0; i < str1.length(); i++) {
String chars = String.valueOf(charArray[i]);
if (str2.contains(chars)) {
c++;
}
}
} else {
per = (str1.length() * 80) / 100; // 80% per match logic
charArray = str2.toCharArray();
for (int i = 0; i < str2.length(); i++) {
String chars = String.valueOf(charArray[i]);
if (str1.contains(chars)) {
c++;
}
}
}
if (c >= per) {
Toast.makeText(getApplicationContext(), "true", 0).show();
} else {
Toast.makeText(getApplicationContext(), "false", 0).show();
}
当大多数字符相似时如何识别两个字符串
我想在这个示例中得到 true
"Hello Wolrld" == "HelloWorld"
或
"hello world!!" == "helloworld"
我知道这些不相等,但是由于大部分字符相同,所以对我来说足够了
提前致谢
您可以计算 Levenshtein distance of the two strings (see for example this C# implementation),然后定义一个阈值,您认为达到该阈值的字符串为 "equal"。
什么是合理的阈值取决于您的要求。可能,将谓词定义为 d <= a * Math.Min(string1.Length, string2.Length)
应该可行,其中 d
是字符串的 Levenshtein 距离,而 a
是 "similarity" 在 0 和 1 之间的一个因子。在您的示例中a==0.3
应该可以。
使用这个
Regex.Replace(textBox1.Text, @"[^0-9a-zA-Z]+", "").ToLower() == your string in lower case
如果您正在寻找非常基本的检查,您可以使用 Zip
枚举字符以比较它们,计算匹配的字母,如果匹配的数量超过特定阈值则报告 true .如果一个字符串是另一个字符串的移位版本,则不会捕获它;它只会在同一索引处捕获共同的字母。
public static class ExtensionMethods
{
public static bool FuzzyCompare(this string lhs, string rhs, float ratioRequired)
{
var matchingLetters = lhs.Zip
(
rhs,
(a,b) => a == b ? 1 : 0
)
.Sum();
return (float)matchingLetters / (float)lhs.Length > ratioRequired;
}
}
要比较两个字符串以查看它们是否匹配至少一半的字母,请传递 0.5 的 ratioRequired
。
public static void Main()
{
var a = "ABCD";
var b = "ABCDEFGHI";
Console.WriteLine( a.FuzzyCompare(b, 0.5F) );
}
输出:
True
如果 80% 的词比较相似,则为真试试这个
String str1 = "Hello world";
String str2 = "Helloworld!!";
char[] charArray;
int per = 0;
int c = 0;
if (str1.length() > str2.length()) {
per = (str1.length() * 80) / 100; // 80% per match logic
charArray = str1.toCharArray();
for (int i = 0; i < str1.length(); i++) {
String chars = String.valueOf(charArray[i]);
if (str2.contains(chars)) {
c++;
}
}
} else {
per = (str1.length() * 80) / 100; // 80% per match logic
charArray = str2.toCharArray();
for (int i = 0; i < str2.length(); i++) {
String chars = String.valueOf(charArray[i]);
if (str1.contains(chars)) {
c++;
}
}
}
if (c >= per) {
Toast.makeText(getApplicationContext(), "true", 0).show();
} else {
Toast.makeText(getApplicationContext(), "false", 0).show();
}