2013-02-27 78 views
0

我有這個功能,我想比較兩個字符串,然後返回有多少單詞存在,但以下不起作用。我似乎總是得到0爲SameWordCount和1爲MasterAddressWordCount查找在兩個字符串中有多少個單詞相同

任何想法?

// 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"; } 

回答

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(' '); 
3

你的問題是因爲new char[' '],你的意思是這裏new char[] {' '}。編譯器(非常有幫助)在這裏將' '轉換爲int,使其成爲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

一個十六進制表示
相關問題