如果我有以下字符串:
string s = "abcdefghab";
那么我如何获得一个字符串(或char []),它只包含使用C#和LINQ在原始字符串中重复的字符.在我的例子中,我想以"ab"结束.
虽然没有必要,但我试图在一行LINQ中做到这一点,到目前为止已经提出:
s.ToCharArray().OrderBy(a => a)...
Andrew Theke.. 7
String text = "dssdfsfadafdsaf"; var repeatedChars = text.ToCharArray().GroupBy(x => x).Where(y => y.Count() > 1).Select(z=>z.Key);
TheSoftwareJ.. 7
string theString = "abcdefghab"; //C# query syntax var qry = (from c in theString.ToCharArray() group c by c into g where g.Count() > 1 select g.Key); //C# pure methods syntax var qry2 = theString.ToCharArray() .GroupBy(c => c) .Where(g => g.Count() > 1) .Select(g => g.Key);
James Curran.. 7
另外,字符串已经是IEnumerable,所以你真的不需要调用ToCharArray();
var qry = (from c in theString group c by c into g where g.Count() > 1 select g.Key);
将qry作为IEnumerable,但如果你真的需要一个char [],就像那样简单qrt.ToArray()
.
String text = "dssdfsfadafdsaf"; var repeatedChars = text.ToCharArray().GroupBy(x => x).Where(y => y.Count() > 1).Select(z=>z.Key);
string theString = "abcdefghab"; //C# query syntax var qry = (from c in theString.ToCharArray() group c by c into g where g.Count() > 1 select g.Key); //C# pure methods syntax var qry2 = theString.ToCharArray() .GroupBy(c => c) .Where(g => g.Count() > 1) .Select(g => g.Key);
另外,字符串已经是IEnumerable,所以你真的不需要调用ToCharArray();
var qry = (from c in theString group c by c into g where g.Count() > 1 select g.Key);
将qry作为IEnumerable,但如果你真的需要一个char [],就像那样简单qrt.ToArray()
.