我有這個功能,我想比較兩個字符串,然後返回有多少單詞存在,但以下不起作用。我似乎總是得到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"; }