我想比較一個字符串和很多字符串。這是如何在C#中完成的?比較一個字符串和幾個不同的字符串
回答
如果你想檢查一個字符串包含在一個字符串列表,你可以使用Contains
擴展方法:
bool isStringContainedInList =
new[] { "string1", "string2", "string3" }.Contains("some string")
但問題是我dono什麼是「一些字符串」我想找到... 如何做到這一點? 找到很多字符串之間的共同點... – user251334 2010-01-15 07:31:38
string[] comparisonList = {"a", "b" "c"};
from s in comparisonList where comparisonList.Contains("b") select s;
我不認爲LINQ在這裏真的有必要。 – 2010-01-15 07:16:54
@musicfreak:meh。 @ash:自LINQ發佈以來已經有4年了。 – 2010-01-15 07:24:04
如果你想比較,使用String.Compare。
如果您在列表中查找字符串,請使用與列表類型等效的Contains/Select方法。
我喜歡使用String.Compare()靜態方法,因爲它可以讓您將所有內容都顯式化。這一點很重要,因爲字符串比較可能因微妙的錯誤而臭名昭着。
例如:
// Populate with your strings
List<string> manyStrings = new List<string>();
string oneString="target string";
foreach(string current in manyStrings)
{
// For a culture aware, safe comparison
int compareResult=String.Compare(current,oneString,
StringComparison.CurrentCulture);
// OR
// For a higher performance comparison
int compareResult=String.Compare(current,oneString,
StringComparison.Ordinal);
if (compareResult==0)
{
// Strings are equal
}
}
如果你真的只想知道一個字符串是否是另一個較大字符串的子,在上面的循環中,您可以使用:
int indexPos=current.IndexOf(oneString,StringComparison.Ordinal);
if (indexPos>=0)
{
// oneString was found in current
}
注意的IndexOf接受相同的有用StringComparison枚舉。
要查找列表中的多個列表中的字符串,您可以開始將這些字符串放入HashSet中,然後檢查每個字符串是否已存在於此集合中。
例如,你可以:
HashSet<string> hashSet = new HashSet<string>();
foreach (string item in myList)
{
if (hashSet.Contains(item))
{
// already in the list
...
}
else
{
// not seen yet, putting it into the hash set
hashSet.Add(item);
}
}
我建議你看看這個維基百科article有關最長公共子串。
我記得從本科生那裏找到最長的公共子字符串的策略,你可以先找到一個稍短的子字符串,然後從那裏(和重複)擴展。也就是說,如果「abcd」是一個常見的子字符串,那麼「abc」也是如此,「ab」也是如此。
這適用於重複算法,首先找到出現在字符串中的所有2個字母對(我不打擾使用一個字母子字符串,因爲對於大數據集,它們將包含整個字母表)。然後你再遍歷查找所有三字母串,等等...
到集合中的所有字符串比較彼此找到重複的,這是最有效的使用詞典:
string[] strings = { "Zaphod", "Trillian", "Zaphod", "Ford", "Arthur" };
var count = new Dictionary<string, int>();
foreach (string s in strings) {
if (count.ContainsKey(s)) {
count[s]++;
} else {
count.Add(s, 1);
}
}
foreach (var item in count) {
Console.WriteLine("{0} : {1}", item.Key, item.Value);
}
輸出:
Zaphod : 2
Trillian : 1
Ford : 1
Arthur : 1
您也可以使用LINQ方法做到這一點:
var count =
strings
.GroupBy(s => s)
.Select(
g => new { Key = g.First(), Value = g.Count() }
);
- 1. 比較兩個字符串[]
- 2. 比較兩個字符串
- 3. 比較兩個字符串?
- 4. 比較一個字符串和一個像字符串這樣的模板
- 5. 如何比較另一個字符串中的字符串?
- 6. 比較與另一個字符串數組中的字符串
- 7. 如何比較字符串值與另一個字符串值?
- 8. SQL - 比較字符串,而不是整個字符串
- 9. 字符串比較和單個字符的字母順序
- 10. 比較一個字符串數組PHP
- 11. 字符串比較上一個布爾
- 12. JavaScript字符串和字符比較
- 13. 哪個`[`不能比較空字符串?
- 14. 比較不同編碼的字符串
- 15. 如何比較包含相同字符的2個字符串
- 16. 與字符串比較字符串值
- 17. string.compare和字符串比較
- 18. ICU和字符串比較
- 19. 不同之處比較字符數組和char字符串的指針字符串字符串
- 20. 字符串比較
- 21. 比較字符串
- 22. 比較字符串
- 23. 字符串比較
- 24. 字符串比較
- 25. 字符串比較
- 26. 比較字符串
- 27. 比較字符串
- 28. 字符串比較
- 29. 比較字符串
- 30. 字符串比較
OK,看完這個任務後,離子幾次,也deepasundaris自己的答案與額外的信息,我認爲他想要以下內容: 我有一個字符串的列表,需要找到一個未知的子字符串,該列表中的每個字符串中存在 – Oliver 2010-01-15 07:42:41
請重新短語問題以反映你真正的意思。 – Yoni 2010-01-15 07:57:34
您能否編輯您的初始問題以提供更多信息?我注意到你在這裏的一些評論中澄清了這個問題,但最好的方法是更新問題本身,這樣人們就不必通過所有的答案來了解你需要什麼。 – 2010-01-15 08:02:48