[英]Compare char in List C#
我想知道是否有任何c#函數檢查字母是否存在然后才存在? 換句話說,我將一個字符串作為參數發送給函數,以檢查一個字母是否存在多次。 例如,字符串“ AABDCK”應返回“ A”。 有什么方法可以使用字典嗎?
有什么方法可以使用字典嗎?
是,遍歷字符串中的每個字符,並跟蹤Dictionary<char, int>
中每個字符的出現次數。
Dictionary<char, int> counts = new Dictionary<char, int>();
foreach (var ch in myString)
{
if (counts.ContainsKey(ch))
{
counts[ch]++;
}
else counts.Add(ch, 1);
}
檢查字典中值大於1的鍵。
您也可以使用Linq執行此操作。 我不在編譯器的前面,但是看起來像
List<char> multipleTimes = myString
.GroupBy(c => c)
.Select(g => new { Character = g.Key, Count = g.Count() })
.Where(a => a.Count > 1)
.Select(a => a.Character)
.ToList();
您可以使用linq進行操作,查看下面的注釋以了解代碼,例如:
public string GetLetterWithMoreOccurrences(string text)
{
// check if the text was provided
if (string.IsNullOrEmpty(letter))
throw new ArgumentException("You must provide a text.", "text");
// if it is lower than 2 chars, return the first one
// I'm not sure if it is what you want, but let's consider it.
if (text.Length <= 2)
return text[0];
// find the first letter
var letter = text.GroupBy(c => c) // group by char
.Select(x => { Letter = x.Key, Total = x.Count() }) // in the group, count how many occurrences each letter has
.OrderByDescending(x => x.Total) // order by the total by descending
.First(); // get the first one
return letter;
}
您可以檢查:
var letter = GetLetterWithMoreOccurrences("AABDCK");
// should return "A"
現在,如果您希望所有出現多個字母的字母,可以嘗試:
public string GetLetterAllDuplicates(string text)
{
// check if the text was provided
if (string.IsNullOrEmpty(letter))
throw new ArgumentException("You must provide a text.", "text");
// if it is lower than 2 chars, return the first one
// I'm not sure if it is what you want, but let's consider it.
if (text.Length <= 2)
return text[0];
// find the first letter
var letters = text.GroupBy(c => c) // group by char
// in the group, count how many occurrences each letter has
.Select(x => { Letter = x.Key, Total = x.Count() })
// get only the occurrences that has more than 1.. (you can change this parameter)
.Where(x => Total > 1)
// get it as array
.ToArray();
var result = string.Join(letters, "");
return result ;
}
並使用它:
var text = GetLetterAllDuplicates("AABKCBD");
// should return "AB"
您可以使用:
String.IndexOf("A");
它將返回第一次出現的索引。 如果返回-1,則不會出現“ A”事件。
這是我的不使用IndexOf的LINQ實現:
string x = "AABCDEF";
List<char> repeatedCharacters = new List<char>();
var groupsOfChars = x.GroupBy(stringCharacter => stringCharacter);
groupsOfChars
.ToList()
.ForEach(item => {
if (item.Count() > 1) repeatedCharacters.Add(item.Key);
});
或者,如果您不需要該組:
string x = "AABCDEF";
List<char> repeatedCharacters = new List<char>();
x.GroupBy(stringCharacter => stringCharacter)
.ToList()
.ForEach(item => {
if (item.Count() > 1) repeatedCharacters.Add(item.Key);
});
然后您可以檢查它:
repeatedCharacters.ForEach(item => {
Console.WriteLine(item.ToString());
});
//Since repeatedCharacters is an array, you can just simply do:
string stringOfRepeatedCharacters = repeatedCharacters.ToString();
//So you can easily convert the values to a String.
//[ 'A', 'B' ] is the result and it can be "AB".
似乎要把更多的木材帶入森林,但似乎有些答案不完整或不完全符合要求,或者太復雜了:)。
string input = "AABZFFZDCZZK";
//can handle null and empty string...
var rslt =
(string.IsNullOrEmpty(input) ? string.Empty : input)
.GroupBy(c => c)
.Select(gc => gc.Count() > 1 ? gc.Key : (char)0)
.Where(c => c != (char)0)
.OrderBy(c => c)//optional
.Aggregate(string.Empty, (c, n) => c + n)
;
結果是:
"AFZ"
問題是要向后字符串提供多次出現的字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.