這將檢查一個字符串是否是另一個字符串的子R(給出在你的例子RESET vs SET)。請注意,它是反射式的,所以RESET vs SET == SET與RESET。如果你需要檢查子字符串(所以RESE vs SET == 2/4 == 0.5或其他),那麼你需要解釋你想要的。
public static double Percentage(string str1, string str2) {
// Handling of empty strings. No divisions by zero here!
if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2)) {
return 0.0;
}
// str2 is contained in str1
if (str1.Contains(str2)) {
return 100.0 * str2.Length/str1.Length;
}
// str1 is contained in str2
if (str2.Contains(str1)) {
return 100.0 * str1.Length/str2.Length;
}
// No matching
return 0.0;
}
目前尚不清楚應該如何完成匹配......子串?字符一個字符? SR是RESET的匹配嗎?舉一些例子。 – xanatos 2015-03-03 12:21:17
正如鏈接的其他問題所寫的,你需要給我們[字符串指標](http://en.wikipedia.org/wiki/String_metric) – xanatos 2015-03-03 12:22:28
不確定,但看起來你基本上正在採取總計 - Levenshtein距離然後計算百分比? – Robert 2015-03-03 12:26:35