【发布时间】:2013-04-07 08:58:18
【问题描述】:
我的教授给全班举了一个 C# 的例子,可以用来从文本文件中分割数据。我正在尝试将它用于涉及拆分 txt 内容的项目。文件分成 4 个数组或字段。代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
class Program
{
static void Main()
{
int i = 0;
foreach (string line in File.ReadAllLines("census.txt"))
{
string[] parts = line.Split(',');
foreach (string part in parts)
{
Console.WriteLine("{0}",
part);
}
i++;
}
}
}
这里是 census.txt:
21,f, s, 14
41,f, m, 22
12, m, s, 12
11, f, s, 8
29, m, m, 4
6, m, s, 12
9, f, s, 2
30, f, s, 1
这应该是按年龄、性别、婚姻状况和地区划分的假设人口普查数据。我不断得到的输出是单行中的每个数字或字符,如下所示:
21
f
s
14
41
f
m
22
等等。
我认为这意味着它正在工作,但我想知道如何使用它来输入 4 个并行数组。我还想了解更多关于将其拆分为 4 个字段、结构或类的信息。项目的下一部分涉及每次出现某个年龄编号或地区编号时进行计数,这将涉及大量数组。
【问题讨论】:
-
我不明白这背后的目的......
标签: c# arrays file struct split