如何阅读和编辑在C#中的.txt文件? [英] How do I read and edit a .txt file in C#?
本文介绍了如何阅读和编辑在C#中的.txt文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有一个txt文件,上面写着:
For example, I have a txt file that reads:
12 345 45
2342 234 45 2 2 45345
234 546 34 3 45 65 765
12 23 434 34 56 76 5
欲插入所有的数字之间逗号,左括号添加到每一行的开头和右括号到每一行的末尾。因此,编辑后应改为:
I want to insert a comma between all the numbers, add a left brace to the begining of each line and a right brace to the end of each line. So after the editing it should read:
{12, 345, 45}
{2342, 234, 45, 2, 2, 45345}
{234, 546, 34, 3, 45, 65, 765}
{12, 23, 434, 34, 56, 76, 5}
我该怎么办呢?
推荐答案
我pretty多琢磨出来的时候所有给我你的答案,但非常感谢反正:)。我想出了这一点:
I pretty much figured it out while you were all giving me your answers, but thanks a lot anyway :). I came up with this:
TextReader reader = new StreamReader("triangle.txt");
TextWriter writer = new StreamWriter("triangle2.txt");
for (; ; )
{
string s = reader.ReadLine();
if (s == null)
break;
s = s.Replace(" ", ", ");
s = "{" + s + "},";
writer.WriteLine(s);
}
这篇关于如何阅读和编辑在C#中的.txt文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文