这是StreamReader
读取的文本文件。
500, 120, 60, 0, 350
100, 230, 0, 50, 0
0, 75, 0, 0, 220
3000, 400, 600, 35, 0
350, 200, 100, 80, 250
0, 285, 325, 150, 75
我想在每行前面添加城市,这样它读起来是这样的:
Atlanta: 500, 120, 60, 0, 350
Baltimore: 100, 230, 0, 50, 0
Chicago:0, 75, 0, 0, 220
Denver: 3000, 400, 600, 35, 0
ELY: 350, 200, 100, 80, 250
Fargo: 0, 285, 325, 150, 75
下面是当前的代码块。
StreamReader sr = new StreamReader("inventory.txt");
String line = sr.ReadToEnd();
Console.WriteLine(line);
如果每行都用""分隔,您可以执行以下操作:
string[] temp = line.Split(new char[] { 'n' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < cityNames.Length; i++)
{
temp[i] += cityNames[i];
}
string result = string.Join(",n", temp);
return result.Remove(result.Length - 3, 2);