gpt4 book ai didi

c# - 解析文本并保存在内存中c#

转载 作者:太空宇宙 更新时间:2023-11-03 11:06:41 25 4
gpt4 key购买 nike

我有这样的文字

 5     1     5     1     5      1     5      1       
1

我必须得到

 5     1     5     1     5      1     5      1       
0 1 0 0 0 0 0 0

并将其保存在内存中。但是当我使用这样的构造时:

List<string> lines=File.ReadLines(fileName);
foreach (string line in lines)
{
var words = line.Split( new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

foreach(string w in words)
Console.Write("{0,6}", w);

// filling out
for (int i = words.Length; i < 8; i++)
Console.Write("{0,6}", "0.");

Console.WriteLine();
}

我只在显示屏上以所需格式打印文本。如何将其保存在 List<string> newLines 中?

最佳答案

如果我们假设数据是等间距的(如您当前的Write 等所建议的那样),那么我会将其作为字符 处理:

char[] chars = new char[49];
foreach(string line in File.ReadLines(path))
{
// copy in the data and pad with spaces
line.CopyTo(0, chars, 0, Math.Min(line.Length,chars.Length));
for (int i = line.Length; i < chars.Length; i++)
chars[i] = ' ';
// check every 6th character - if space replace with zero
for (int i = 1; i < chars.Length; i += 6) if (chars[i] == ' ')
chars[i] = '0';
Console.WriteLine(chars);
}

或者如果你真的需要它作为行,使用(在每次循环迭代结束时):

list.Add(new string(chars));

关于c# - 解析文本并保存在内存中c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15632181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com