aparna chowdary
aparna chowdary

Reputation: 117

c# replace last character in every line

How can I replace the last character in every line?

Example:

rtt45|20160706|N2413847|aneess kim|20160727|
rtt45|20160706|N2247673|ram thomus|20160729|
rtt45|20160706|N2373039|rohan kumar|20160721|

I have tried

string rr = "D:\\temp\\test_07272016020733.txt";
string lines = File.ReadAllText(rr);
lines =lines.Replace("| \n", "\n");

Upvotes: 1

Views: 586

Answers (2)

fubo
fubo

Reputation: 46005

replace

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

with

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

or (equal)

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

Upvotes: 1

Scott Perham
Scott Perham

Reputation: 2470

How about something like:

string rr = "D:\\temp\\test_07272016020733.txt";
string[] lines = File.ReadAllLines(rr);
lines = lines.Select(x => x.TrimEnd('|')).ToArray();

EDIT: If you want it all in a single string to end with:

var text = string.join(Environment.NewLine, lines);

For completeness, in a single line keeping variable names in tact:

string rr = "D:\\temp\\test_07272016020733.txt";    
string lines = string.Join(Environment.NewLine, File.ReadLines(rr).Select(x => x.TrimEnd('|')));

Upvotes: 2

Related Questions