NS01 EW24
$1.04
$1.32
20
NS04 EW21
$1.02
$1.62
9
其中NSxx和EWxx始终为4个字符, 钱总是3位数字,带有美元符号,并且 最后一个数字可以是1或2位数字。
问题:如何将NSxx分为1个列表,将EWxx分为另一个列表,将第一个货币放入另一个列表,第二个货币放入另一个列表,最后一个数字放入另一个列表?
(在很长的文本文件中,还有其他NSxx和EWxx,分别是CCxx,CGxx,DTxx,NExx)
using (StreamReader fare = new StreamReader("fare.txt"))
{
string line;
while ((line = fare.ReadLine()) != null)
{
}
}
答案 0 :(得分:1)
我不会使用其他列表,而是创建一个能够存储一个数据记录的类。
public class Data
{
public string Code1 { get; set; }
public string Code2 { get; set; }
public decimal Price1 { get; set; }
public decimal Price2 { get; set; }
public int Number { get; set; }
}
但是更好的名字。 (我不知道这是什么样的数据。)
和假设文件总是包含表示一个数据记录的4行的块
var fare = File.ReadLines("fare.txt").GetEnumerator();
var list = new List<Data>();
while (fare.MoveNext()) {
if (!String.IsNullOrEmpty(fare.Current)) { // Not an empty line at the end of the file.
var data = new Data();
data.Code1 = fare.Current.Substring(1, 3);
data.Code2 = fare.Current.Substring(5, 3);
fare.MoveNext();
data.Price1 = Decimal.Parse(fare.Current.Substring(1)); // Skip the $ sign.
fare.MoveNext();
data.Price2 = Decimal.Parse(fare.Current.Substring(1));
fare.MoveNext();
data.Number = Int32.Parse(fare.Current);
list.Add(data);
}
}