有人可以告诉我如何在C#中从文本文件中读取特定字符后插入断行。我有一个像
这样的文本文件string text = File.ReadAllText(@" T:\ data.txt");
它包含一个非常大的一行文本(下面只是其中的一小部分。
"dep-1" : "M579.01303,612.20699L578.77297,612.20699 L578.77297,611.967L579.01303 Z","dep-2" : "M579.01303,612.20699L578.77297,612.20699 L578.77297,611.967L579.01303 Z","dep-3" : "M579.01303,612.20699L578.77297,612.20699 L578.77297,611.967L579.01303 Z"
现在我需要在 Z",之后输入分隔线并保存文件。文本将格式化为:
"dep-1" : "M579.01303,612.20699L578.77297,612.20699 L578.77297,611.967L579.01303 Z",
"dep-2" : "M579.01303,612.20699L578.77297,612.20699 L578.77297,611.967L579.01303 Z",
"dep-3" : "M579.01303,612.20699L578.77297,612.20699 L578.77297,611.967L579.01303 Z"
如果C#中有可能,请告诉我吗?
由于
答案 0 :(得分:4)
string[] lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
lines[i] = lines[i].Replace("Z\",", "Z\"," + Environment.NewLine);
File.WriteAllLines(lines, path);
基本的想法是你阅读所有的行,然后你只需要更换Z&#34;与Z&#34;加一个换行符。之后,您将所有行写入同一位置。
好的,这是我的编辑,当所有文本都是同一行时,(该死的技巧问题);
string file = File.ReadAllText(path);
file.Replace("Z\",", "Z\"," + Environment.NewLine);
File.WriteAllText(path);
答案 1 :(得分:1)
你可以试试这个:
string text = File.ReadAllText(@"T:\data.txt");
string newText = string.Join("Z\", \r\n",
text.Split(new[] { "Z\"," }, StringSplitOptions.RemoveEmptyEntries));
File.WriteAllText("path", newText);
答案 2 :(得分:-2)
简单地写一下:
text = text.Replace("Z", "Z\n");