Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# insert string in multiline string

I have a multiline string (from a txt-file using ReadAllText). the string looks like this:

R;0035709310000026542510X0715;;;  
R;0035709310000045094410P1245;;;  
R;0035709310000026502910Z1153;;;

I want to put in a ";" in each line on place 22, so it looks like this:

R;00357093100000265425;10X0715;;;  
R;00357093100000450944;10P1245;;;  
R;00357093100000265029;10Z1153;;;

The multiline string always contain the samme amount of data but not always 3 lines - sometimes more lines.

How do I make this? Please show some code.

Thanks alot :-) Best regards Bent

like image 408
Bent Fisker Avatar asked Feb 23 '26 06:02

Bent Fisker


2 Answers

Try this ...

using System.IO;
using System.Linq;

var lines = File.ReadAllLines("data.txt");
var results = lines.Select(x => x.Insert(22, ";"));
like image 132
Antony Scott Avatar answered Feb 25 '26 20:02

Antony Scott


Step 1, don't use ReadAllText(). Use ReadAllLines() instead.

 string[] myLinesArray = File.ReadAllLines(...);

Step 2, replace all lines (strings) with the changed version.

for(int i = 0; i < myLinesArray.Length; i++)
    myLinesArray[i] = myLinesArray[i].Insert(22, ";");

Step 3, Use WriteAllLines()

like image 41
Henk Holterman Avatar answered Feb 25 '26 19:02

Henk Holterman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!