提前C#字符串比较

时间:2011-08-11 19:14:35

标签: c# .net 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 :)

7 个答案:

答案 0 :(得分:12)

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

Levenshtein Distance

答案 1 :(得分:6)

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

<强>用法:

GetStringPercentage("I have a black car", "I have a car that is small");

方式:

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());
     }

}

答案 2 :(得分:1)

答案 3 :(得分:0)

不,没有。你必须实现自己的。

答案 4 :(得分:0)

只是一个建议,但你能不能把两个字符串都比较,然后根据匹配字符的数量来定义百分比?

答案 5 :(得分:0)

试试这个:

public static int MatchingFunction(string s1, string s2, bool duplicate, bool keySensitive)
{

    if (!keySensitive)
    {
        s1 = s1.ToLower();
        s2 = s2.ToLower();
    }

    List<string> ls1 = null;
    s2 = s2.Trim();

    if (duplicate)
    {
        ls1 = s1.Trim().Split(' ').ToList();
    }
    else
    {
        ls1 = new List<string>();
        string[] as1 = s1.Trim().Split(' ');
        foreach (string s in as1)
            if (!ls1.Contains(s))
                ls1.Add(s);

        string[] as2 = s2.Trim().Split(' ');
        s2 = string.Empty;
        foreach (string s in as2)
            if (!s2.Contains(s))
                s2 = string.Format("{0} {1}", s2, s);
    }


    int has = 0;
    s2 = string.Format("@{0}@", s2.Replace(' ', '@');
    foreach (string s in ls1)
        has += s2.Contains(string.Format("@{0}@", s)) ? 1 : 0;

    return (has * 100 / ls1.Count());
}


string s1 =  " I have a black car";
string s2 = "I have a car that is small";

int p = MatchingFunction(s1, s2, false, false);

答案 6 :(得分:0)

使用http://www.dotnetperls.com/levenshtein中的代码作为基础,我将其修改为返回%而不是数字:

    public static int Compute(string word1, string word2)
    {
        int n = word1.Length;
        int m = word2.Length;
        int[,] d = new int[n + 1, m + 1];

        // Step 1
        if (n == 0)
        {
            return m;
        }

        if (m == 0)
        {
            return n;
        }

        // Step 2
        for (int i = 0; i <= n; d[i, 0] = i++)
        {
        }

        for (int j = 0; j <= m; d[0, j] = j++)
        {
        }

        // Step 3
        for (int i = 1; i <= n; i++)
        {
            //Step 4
            for (int j = 1; j <= m; j++)
            {
                // Step 5
                int cost = (word2[j - 1] == word1[i - 1]) ? 0 : 1;

                // Step 6
                d[i, j] = Math.Min(
                    Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                    d[i - 1, j - 1] + cost);
            }
        }
        // Step 7
        decimal changesRequired = d[n, m];

        //Find the longest word and calculate the percentage equality
        if (word1.Length > word2.Length)
            return Convert.ToInt32(100 - (changesRequired / word1.Length) * 100);
        else
            return Convert.ToInt32(100 - (changesRequired / word2.Length) * 100);
    }

希望这有帮助。