Reading and grouping char values from a source file
I have stumbled upon a problem when creating a program of mine.
I have produced a text file called "values.dat", and within this file I have simply added characters such as below:
Z
D
H
V
Q
Z
D
H
.... a开发者_运维百科nd so on for example. I am using C# Language with visual studio 2010. regards.
I know how to read them into my program, but I do not know the how to GROUP these char values together such as that there will be "AAA" and "BBB" and "CCC" etc instead of having the above set of data in my example.
Could anyone help in actually grouping these char files?
If you are using C# you can try using GroupBy:
string text = File.ReadAllText("values.dat");
var groups = ((IEnumerable<char>)text)
.GroupBy(c => c)
.Select(g => string.Concat(g.ToArray()));
To print it to the console:
foreach (string group in groups)
{
Console.WriteLine(group);
}
精彩评论