开发者

Reading and grouping char values from a source file

开发者 https://www.devze.com 2023-01-25 00:05 出处:网络
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:

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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消