如何替换每行中的最后一个字符?
示例:
rtt45|20160706|N2413847|aneess kim|20160727|
rtt45|20160706|N2247673|ram thomus|20160729|
rtt45|20160706|N2373039|rohan kumar|20160721|
我试过了
string rr = "D:\\temp\\test_07272016020733.txt";
string lines = File.ReadAllText(rr);
lines =lines.Replace("| \n", "\n");
答案 0 :(得分:2)
如下:
string rr = "D:\\temp\\test_07272016020733.txt";
string[] lines = File.ReadAllLines(rr);
lines = lines.Select(x => x.TrimEnd('|')).ToArray();
编辑:如果你想要一个字符串中的所有字符串以:
结束var text = string.join(Environment.NewLine, lines);
为了完整性,在一行中保持变量名称:
string rr = "D:\\temp\\test_07272016020733.txt";
string lines = string.Join(Environment.NewLine, File.ReadLines(rr).Select(x => x.TrimEnd('|')));
答案 1 :(得分:1)
替换
lines = lines.Replace("| \n", "\n");
带
lines = lines.Replace("|" + System.Environment.NewLine, System.Environment.NewLine);
或(等于)
lines = lines.Replace("|\r\n", "\r\n");