在文件行中的特定位置替换charset

时间:2013-08-19 11:31:14

标签: c# string replace

我有一个加载了我的程序的文件,我需要做的是例如:

[前]

1111111111     2222222222       3333    30
1111111111     2222222222       3333    20
1111111111     2222222222       3333    10
1111111111     2222222222       3333   150
1111111111     2222222222       3333   260
1111111111     2222222222       3333   370

[后]

1111111111     2222222222       3333    30
1111111111     2222222222       3333    20
1111111111     2222222222       3333     9 <-- The change is here
1111111111     2222222222       3333   150
1111111111     2222222222       3333   260
1111111111     2222222222       3333   370

我必须替换最后的数字并保存文件,当我点击按钮时会发生这种情况。

程序找到请求的行,我正在使用

string[] myString = File.ReadAllLines(@"fileLocation");

我想我必须使用StreamReader或类似的东西,这对我来说非常重要,因为该文件是我的程序的数据库,并且值一直在变化。

好吧,当我点击按钮时,有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这是我能想到的最简单的方法。 您应该使用除随机数量的空格之外的其他内容来拆分值,因为如果可以静态定义位置,则更容易替换值。

所以你必须知道在哪里改变价值 例如

string[] myString = File.ReadAllLines(fileLocation);

   string[] tmp = myString[2].split(' ');//spli each line on space
   tmp[tmp.length-1] = "9"; //This is where you change out the last number
   myString[a] = String.Join(" ", tmp); //join the new content on the line in the text

File.WriteAllLines(fileLocation, myString);//save everything again