C#读取txt文件并将数据存储在格式化数组中 [英] C# read txt file and store the data in formatted array
问题描述
我有一个文本文件,其中包含以下内容
I have a text file which contains following
Name address phone salary
Jack Boston 923-433-666 10000
所有字段均由空格分隔.
all the fields are delimited by the spaces.
我正在尝试编写C#程序,该程序应读取此文本文件,然后将其存储在格式化的数组中.
I am trying to write a C# program, this program should read a this text file and then store it in the formatted array.
我的数组如下:
address
salary
每当我尝试在Google中查找内容时,我都会得到如何在C#中读写文本文件的信息.
When ever I am trying to look in google I get is how to read and write a text file in C#.
非常感谢您的宝贵时间.
Thank you very much for your time.
推荐答案
您可以使用File.ReadAllLines方法将文件加载到数组中.然后,您可以使用for循环遍历各行,并使用字符串类型的Split方法将每一行分隔到另一个数组中,并将值存储在格式化的数组中.
You can use File.ReadAllLines method to load the file into an array. You can then use a for loop to loop through the lines, and the string type's Split method to separate each line into another array, and store the values in your formatted array.
类似的东西:
static void Main(string[] args)
{
var lines = File.ReadAllLines("filename.txt");
for (int i = 0; i < lines.Length; i++)
{
var fields = lines[i].Split(' ');
}
}
这篇关于C#读取txt文件并将数据存储在格式化数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!