繁体   English   中英

c#替换每一行中的最后一个字符

[英]c# replace last character in every line

如何替换每行中的最后一个字符?

例:

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");

怎么样的:

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('|')));

更换

lines = lines.Replace("| \n", "\n");

lines = lines.Replace("|" + System.Environment.NewLine, System.Environment.NewLine);

或(等于)

lines = lines.Replace("|\r\n", "\r\n");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM