0

I have this function where I would like to compare two strings and then return how many words exist but the following isn't working. I always seem to get 0 for SameWordCount and 1 for MasterAddressWordCount

Any ideas?

// 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"; }
BJ Myers
  • 6,617
  • 6
  • 34
  • 50
Abu Dina
  • 207
  • 1
  • 5
  • 12

2 Answers2

3

Your issue is because of new char[' '], what you meant here was new char[] {' '}. The compiler is (very helpfully) converting ' ' to an int here, making it char[int]. This means that this:

new char[' ']

Is really the same as:

new char[32]

Which ends up being a big useless char[] array, rather than the single space you were after.


You can see this cleanly by looking at the IL generated for:

var a = new char[' '];

Which is:

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

20 being a hex representation of 32.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
0

I've solved the problem by changing the following lines:

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

To:

string[] masterAddressSeparateWords = mastermkAddressKey.Split(' ');
string[] duplicateAddressSeparateWords = duplicatemkAddressKey.Split(' ');
Abu Dina
  • 207
  • 1
  • 5
  • 12