提前C#字符串比较

ang*_*a d 3 .net c# windows

.Net中是否有任何类(函数)可以执行此操作:

如果 s1 = " I have a black car" and s2 = "I have a car that is small";
int matchingProcentage = matchingFunction(s1,s2);

  matchingProcentage == 70% <-- just as an example value :)
Run Code Online (Sandbox Code Playgroud)

Ben*_*gel 12

这是一个很好的方式来实现它!

Levenshtein距离


Rio*_*ams 6

像下面这样的函数应该有效,它是匆匆写的,所以随时改变:

用法:

GetStringPercentage("I have a black car", "I have a car that is small");
Run Code Online (Sandbox Code Playgroud)

方法:

public static decimal GetStringPercentage(string s1, string s2)
{
     decimal matches = 0.0m;
     List<string> s1Split = s1.Split(' ').ToList();
     List<string> s2Split = s2.Split(' ').ToList();

     if (s1Split.Count() > s2Split.Count())
     {
         foreach (string s in s1Split)
             if (s2Split.Any(st => st == s))
                 matches++;

             return (matches / s1Split.Count());
     }
     else
     {
         foreach (string s in s2Split)
             if (s1Split.Any(st => st == s))
                  matches++;

         return (matches / s2Split.Count());
     }

}
Run Code Online (Sandbox Code Playgroud)