C# StreamReader 使用分隔符保存到数组

发布于 2024-12-24 18:16:23 字数 268 浏览 0 评论 0原文

我有一个文本文件,其中包含制表符分隔的数据。我在 C# 应用程序中需要的是从文本文件中读取一行并将它们保存到一个数组中,在每个 \t 处将它们分开。然后我对下一行做同样的事情。

我的代码:

StreamReader sr = new StreamReader(dlg.FileName);
string s = sr.ReadLine();

现在,我已经尝试将该行写入数组,但这不起作用。有人知道如何管理这个吗?

I´ve got a text file with tabulator separated data. What I need in my C# application is that I read one line from the text file and save them to an array, separate them at the each \t. Then I do the same thing with the next row.

My code:

StreamReader sr = new StreamReader(dlg.FileName);
string s = sr.ReadLine();

Now, I already tried to write the line into an array but that doesn´t work. Does anyone one how to manage this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

小…楫夜泊 2024-12-31 18:16:23

的数组

string[] parts = s.Split('\t');

使用 Split 方法创建行 请参阅此处有关 Split() 的文档

Use the Split method to create an Array of the line

string[] parts = s.Split('\t');

See Documentation on Split() here

百变从容 2024-12-31 18:16:23
    foreach (string line in System.IO.File.ReadAllLines(dlg.FileName))
    {
        var myArray = line.Split('\t');
    }
    foreach (string line in System.IO.File.ReadAllLines(dlg.FileName))
    {
        var myArray = line.Split('\t');
    }
活泼老夫 2024-12-31 18:16:23

s.Split('\t') 将按制表符拆分字符串,并创建一个具有适当长度的 string[]

s.Split('\t') will split your string by the tabulator character, and create a string[] with appropriate length.

维持三分热 2024-12-31 18:16:23

修改示例代码:

StreamReader sr = new StreamReader(dlg.FileName); 
string s = sr.ReadLine(); 
var items = s.Split('\t'); 

最后,items 包含一个字符串数组,表示选项卡之间的字符。选项卡不包含在数组中。该数组可能包含空元素(对于两个连续选项卡的情况)。

Ammending your example code:

StreamReader sr = new StreamReader(dlg.FileName); 
string s = sr.ReadLine(); 
var items = s.Split('\t'); 

In the end, items contains an array of strings that represent the characters between the tabs. The tabs are not included in the array. The array may contain empty elements (for the case of two consecutive tabs).

懵少女 2024-12-31 18:16:23
StreamReader reader = new StreamReader("input.txt");
string[] content = reader.ReadToEnd().Replace("\n","").Split('\t');

如果你想保留新行的比

string[] content = reader.ReadToEnd().Split('\t');

StreamReader reader = new StreamReader("input.txt");
string[] content = reader.ReadToEnd().Replace("\n","").Split('\t');

if you want to keep New Line's than

string[] content = reader.ReadToEnd().Split('\t');

我早已燃尽 2024-12-31 18:16:23

在下面的示例中,items 将是一个包含每行文本值的 String[]。它在每次迭代时被覆盖,因此您需要在循环内对其执行某些操作。将其保存到更大的集合中,将其写入文件等......

StreamReader sr = new StreamReader(dlg.FileName);
while (sr.Peek() >= 0) {
    var line = sr.ReadLine();
    var items = line.Split(new Char[] { '\t' });
}

In the example below, items will be a String[] containing each line of text's values. It will be overwritten with each iteration so you'll want to do something with it inside the loop. Save it to a larger collection, write it to a file, etc...

StreamReader sr = new StreamReader(dlg.FileName);
while (sr.Peek() >= 0) {
    var line = sr.ReadLine();
    var items = line.Split(new Char[] { '\t' });
}
壹場煙雨 2024-12-31 18:16:23

如果文件仅包含一行,则使用:

string[] myArray = s.Split('\t');

If the file contains only one line, then use:

string[] myArray = s.Split('\t');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文