Home > OS >  Add a character to the end of each line in a txt string expression
Add a character to the end of each line in a txt string expression

Time:10-12

EASI*2.0*01*105687859*01*112088708*20100809*080953*EDT*T*850*080908095349328 \r\n
01*850*00*1.0*SA*C0020287*10*20020207*122325*A123456*USD*34579*0005*707738*John Doe 314-999-1279*PP*BK*FT*P2**2.25*10*30*20021231*Net 60*20100809*20020331*FEDX*Alpha Philadelphia*John Smith*123 ABC Drive**Hebron*OH*43025*US*N*Deliver to Receiving Dock before 8:00 AM, call before delivery*Shrink Wrap cartons*UKK*PROMO001*92*212-345-678 \r\n
02*1*00999913060595*K112*54*7*36*EA*8.3600*372.96 \r\n
02*1*00884913060595*K112*54*7*36*EA*8.3600*372.96 \r\n
02*1*00884913060595*K112*54*7*36*EA*8.3600*372.96 \r\n09*5*120**** \r\n
EASX*080908095349328*01 \r\n

\r\n is the expressions indicating a line is finished. My goal is to add a special character before these characters, for example, ~

Here's the result I'm trying to get.

EASI*2.0*01*105687859*01*112088708*20100809*080953*EDT*T*850*080908095349328~\r\n
01*850*00*1.0*SA*C0020287*10*20020207*122325*A123456*USD*34579*0005*707738*John~Doe~314-999-1279*PP*BK*FT*P2**2.25*10*30*20021231*Net~60*20100809*20020331*FEDX*Alpha~Philadelphia*John~Smith*123~ABC~Drive**Hebron*OH*43025*US*N*Deliver~to~Receiving~Dock~before~8:00~AM,~call~before~delivery*Shrink~Wrap~cartons*UKK*PROMO001*92*212-345-678~\r\n
02*1*00999913060595*K112*54*7*36*EA*8.3600*372.96~\r\n
02*1*00884913060595*K112*54*7*36*EA*8.3600*372.96~\r\n
02*1*00884913060595*K112*54*7*36*EA*8.3600*372.96~\r\n09*5*120****~\r\n
EASX*080908095349328*01~\r\n

Note we have also lost a trailing space (if it exists) on each line. How can I do this?

CodePudding user response:

You can simply use a Replace statement e.g.:

string input = "EASI*2.0*01*105687859*01*112088708*20100809*080953*EDT*T*850*080908095349328 \r\n01*850*00*1.0*SA*C0020287*10*20020207*122325*A123456*USD*34579*0005*707738*John Doe 314-999-1279*PP*BK*FT*P2**2.25*10*30*20021231*Net 60*20100809*20020331*FEDX*Alpha Philadelphia*John Smith*123 ABC Drive**Hebron*OH*43025*US*N*Deliver to Receiving Dock before 8:00 AM, call before delivery*Shrink Wrap cartons*UKK*PROMO001*92*212-345-678 \r\n02*1*00999913060595*K112*54*7*36*EA*8.3600*372.96 \r\n02*1*00884913060595*K112*54*7*36*EA*8.3600*372.96 \r\n02*1*00884913060595*K112*54*7*36*EA*8.3600*372.96 \r\n09*5*120**** \r\nEASX*080908095349328*01 \r\n";
string output = input.Replace(" \r\n", "~\r\n");

But if the character infront of the \r\n is not always a space you have to use a Regex e.g.:

string input = "EASI*2.0*01*105687859*01*112088708*20100809*080953*EDT*T*850*080908095349328 \r\n01*850*00*1.0*SA*C0020287*10*20020207*122325*A123456*USD*34579*0005*707738*John Doe 314-999-1279*PP*BK*FT*P2**2.25*10*30*20021231*Net 60*20100809*20020331*FEDX*Alpha Philadelphia*John Smith*123 ABC Drive**Hebron*OH*43025*US*N*Deliver to Receiving Dock before 8:00 AM, call before delivery*Shrink Wrap cartons*UKK*PROMO001*92*212-345-678 \r\n02*1*00999913060595*K112*54*7*36*EA*8.3600*372.96 \r\n02*1*00884913060595*K112*54*7*36*EA*8.3600*372.96 \r\n02*1*00884913060595*K112*54*7*36*EA*8.3600*372.96 \r\n09*5*120**** \r\nEASX*080908095349328*01 \r\n";
string output = Regex.Replace(input, ".\r\n", "~\r\n");

To figure out how Regex work use a online tool like: https://regex101.com/

Also do not forget to add the using statement: using System.Text.RegularExpressions;

CodePudding user response:

Try this:

var lines = File.ReadLines("myFileName.txt"). //open the file
          Select(line => line.TrimEnd() "~"). //make the adjustment
          ToArray();                          //finish reading the file, 
                                              // so it won't be locked for writing
File.WriteAllLines("myFileName.txt", lines);  //save the changed lines.

Alternatively, you could save a lot of memory and cpu by removing the ToArray() call, writing to a different file, and when the write is complete delete the original and rename the saved file to take it's place.

  • Related