Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
高级C#字符串比较_C#_.net_Windows - Fatal编程技术网

高级C#字符串比较

高级C#字符串比较,c#,.net,windows,C#,.net,Windows,.Net中是否有任何类(函数)可以执行此操作: 如果 s1=“我有一辆黑色汽车”和s2=“我有一辆小型汽车” int matchingProcentage=matchingFunction(s1,s2) matchingProcentage==70%不,没有。您必须实现自己的功能。这里有一个很好的方法 这只是一个建议,但您能同时使用两个字符串并比较字符与字符,并根据匹配字符的数量定义一个百分比吗?您可以使用类似以下的函数,它是匆忙编写的,因此可以随意更改: 用法: GetStringPercen

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

如果
s1=“我有一辆黑色汽车”和s2=“我有一辆小型汽车”
int matchingProcentage=matchingFunction(s1,s2)


matchingProcentage==70%不,没有。您必须实现自己的功能。

这里有一个很好的方法


这只是一个建议,但您能同时使用两个字符串并比较字符与字符,并根据匹配字符的数量定义一个百分比吗?

您可以使用类似以下的函数,它是匆忙编写的,因此可以随意更改:

用法:

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

}
方法:

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

}
public静态十进制GetStringPercentage(字符串s1、字符串s2)
{
十进制匹配=0.0m;
列表s1Split=s1.Split(“”).ToList();
列表s2Split=s2.Split(“”).ToList();
如果(s1Split.Count()>s2Split.Count())
{
foreach(s1Split中的字符串s)
如果(s2Split.Any(st=>st==s))
匹配++;
返回(匹配/s1Split.Count());
}
其他的
{
foreach(s2Split中的字符串s)
如果(s1Split.Any(st=>st==s))
匹配++;
返回(匹配/s2Split.Count());
}
}
试试这个:

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);
公共静态整型匹配函数(字符串s1、字符串s2、布尔重复、布尔键敏感)
{
如果(!键敏感)
{
s1=s1.ToLower();
s2=s2.ToLower();
}
列表ls1=null;
s2=s2.Trim();
如果(重复)
{
ls1=s1.Trim().Split(“”).ToList();
}
其他的
{
ls1=新列表();
字符串[]as1=s1.Trim().Split(“”);
foreach(as1中的字符串s)
如果(!ls1.包含)
ls1.添加(s);
字符串[]as2=s2.Trim().Split(“”);
s2=字符串。空;
foreach(as2中的字符串s)
如果(!s2.包含)
s2=string.Format(“{0}{1}”,s2,s);
}
int has=0;
s2=string.Format(“@{0}@”),s2.Replace(“”,@');
foreach(ls1中的字符串s)
has+=s2.Contains(string.Format(“@{0}@”,s))?1:0;
return(has*100/ls1.Count());
}
string s1=“我有一辆黑色汽车”;
string s2=“我有一辆很小的车”;
int p=匹配函数(s1,s2,false,false);
使用在找到的代码作为基数,我修改它以返回一个%而不是一个数字:

    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);
    }
publicstaticint计算(stringword1,stringword2)
{
int n=word1.长度;
int m=单词2.长度;
int[,]d=新的int[n+1,m+1];
//第一步
如果(n==0)
{
返回m;
}
如果(m==0)
{
返回n;
}
//步骤2

对于(int i=0;i不,不是。但是应该很简单:只要比较字符,当它们不同时,你就有了你的百分比。@Tomas这个例子怎么样:“123456790”和“234567890”?它们是0%相同还是90%相同(或其他数字?),而不定义什么“匹配”实际上,这意味着没有答案,这可能就是为什么没有内置方法的原因。@dlev由OP来定义它。@Tomas Right。他们没有这样做。+1,这是您应该使用的。我在下面发布了我的“修订”代码,返回的是%而不是数字。