0

我有这个函数,我想比较两个字符串,然后返回存在多少个单词,但以下不起作用。我似乎总是得到 SameWordCount 的 0 和 MasterAddressWordCount 的 1

有任何想法吗?

// some more string cleaning
        mastermkAddressKey = mastermkAddressKey.Replace(",", " ").Replace(".", " ").Trim();
        mastermkAddressKey = Encoding.ASCII.GetString(Encoding.GetEncoding("Cyrillic").GetBytes(mastermkAddressKey));
        mastermkAddressKey = mastermkAddressKey.Replace("  ", " |").Replace("| ", "").Replace("|", "");
        mastermkAddressKey = QbaseStrings.RemoveDuplicateWords(mastermkAddressKey);

        duplicatemkAddressKey = duplicatemkAddressKey.Replace(",", " ").Replace(".", " ").Trim();
        duplicatemkAddressKey = Encoding.ASCII.GetString(Encoding.GetEncoding("Cyrillic").GetBytes(duplicatemkAddressKey));
        duplicatemkAddressKey = duplicatemkAddressKey.Replace("  ", " |").Replace("| ", "").Replace("|", "");
        duplicatemkAddressKey = QbaseStrings.RemoveDuplicateWords(duplicatemkAddressKey);

        string[] masterAddressSeparateWords = mastermkAddressKey.Split(new char[' '], StringSplitOptions.RemoveEmptyEntries);
        string[] duplicateAddressSeparateWords = duplicatemkAddressKey.Split(new char[' '], StringSplitOptions.RemoveEmptyEntries);

        int SameWordCount = 0;
        int MasterAddressWordCount = 0;

        foreach (string masterWord in masterAddressSeparateWords)
                {
                    foreach (string duplicateWord in duplicateAddressSeparateWords)
                    {
                        if (masterWord == duplicateWord) {SameWordCount++;}
                    }

                    MasterAddressWordCount++;
                }

        int WordDifference = MasterAddressWordCount - SameWordCount;

        if (WordDifference == 0) { return "sure"; }
        if (WordDifference > 0 && WordDifference < 3) { return SameWordCount.ToString() + " " + MasterAddressWordCount.ToString(); }
        if (WordDifference > 2 && WordDifference < 5) { return "possible"; }
4

2 回答 2

3

你的问题是因为new char[' '],你在这里的意思是new char[] {' '}。编译器(非常有帮助)转换' 'inthere,使其成为char[int]. 这意味着:

new char[' ']

真的是一样的:

new char[32]

最终成为一个无用的大char[]数组,而不是您所追求的单个空间。


您可以通过查看为以下内容生成的 IL 清楚地看到这一点:

var a = new char[' '];

这是:

IL_0001:  ldc.i4.s    20
IL_0003:  newarr      System.Char
IL_0008:  stloc.0     // a

20 是 32 的十六进制表示。

于 2014-02-20T02:18:28.037 回答
0

我通过更改以下几行解决了这个问题:

string[] masterAddressSeparateWords = mastermkAddressKey.Split(new char[' '], StringSplitOptions.RemoveEmptyEntries);
        string[] duplicateAddressSeparateWords = duplicatemkAddressKey.Split(new char[' '], StringSplitOptions.RemoveEmptyEntries);

至:

string[] masterAddressSeparateWords = mastermkAddressKey.Split(' ');
string[] duplicateAddressSeparateWords = duplicatemkAddressKey.Split(' ');
于 2013-02-27T10:49:13.447 回答