【问题标题】:Read specific columns in a text file读取文本文件中的特定列
【发布时间】:2017-10-13 22:42:12
【问题描述】:

我在 C:\Users\Volki\Documents\Ciel\Members\Cielmember.txt 位置有一个文本文件。 文本文件内容为 2 行,如下所示:
code1=EX386MF06BR
代码2=DB45ZE45GT5

我正在寻找的是忽略字符“=”(code1/code2)之前的内容,然后开始读取每一行中的列(逐列),并在 Console.WriteLine() 中显示每一列值。

这是我尝试过的方法,但它不起作用。

if (File.Exists(@"C:\Users\Volki\Desktop\Test\testX.txt"))
{
    string[] lines = File.ReadAllLines(@filename);
    for (int y = 0; y < lines.Length; y++)
    {
        Console.WriteLine(lines[y]);
        string[] columns = lines[y].Split('\t');
        for (int z = 0; z < columns.Length; z++)
        {
            Console.WriteLine(z + ":" + columns[z]);
            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 我看到您正在使用split 将文件拆分为新行,为什么不使用相同的技术将每一行拆分为=?然后访问第二个元素:split(columns[z], '=')[1]

标签: c# file text multiple-columns


【解决方案1】:

您是否尝试过根据“=”分割线

if (File.Exists(@"C:\Users\Volki\Desktop\Test\testX.txt"))
{
       string[] lines = File.ReadAllLines(@filename);
        for (int y = 0; y < lines.Length; y++)
        {
                Console.WriteLine(lines[y]);
                 string[] columns = lines[y].Split('=');
                // Columns should have two entries, one for codeX and another one for value. Since you only need the latter, print the second entry in columns 
                Console.WriteLine(columns[1]);
                Console.ReadKey();
            }
       }
}

【讨论】:

  • 我想我没有问正确的问题。我想要的是逐个字符而不是逐列,
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多