0

I have a c# app that reads a text file and looks for ^ characters and replaces them with \t. It also looks for | characters and attempts to replace them with \n. This .txt file should then be copied into a spreadsheet. The problem is that when the app replaces | it creates a new row of cells. I want it kept in the same cell.

For example the input is:

This^is^testing^to^see|if|it|works

The output I need is:

This    is    testing    to    see 
                               if
                               it
                               works

Instead I get:

This    is    testing    to    see 
if
it
works

I am currently using:

 File.WriteAllText(textBox1.Text, File.ReadAllText(textBox1.Text).Replace("|", "\n"));
 File.WriteAllText(textBox1.Text, File.ReadAllText(textBox1.Text).Replace("^", "\t"));

Where am I slipping up?

The closure of this question was of no help. \r\n does the same thing. I need the replacement of the | delimiter to keep the information in the same cell. As you can see it the above example it is moving it a row down into the A (first) column thanks to the \n I assume.

WadeG
  • 61
  • 2
  • 8
  • What output format are you creating? csv can escape newlines, if you quote fields correctly. Tab delimited text wont work. – Jeremy Lakeman May 19 '21 at 07:25
  • You need to use proper CSV-aware code to write these values, so that the newlines you want to keep in a single field of a single row do in fact stay there. The way you're doing it now, the newlines denote a whole new row of data (as you can see). See duplicate for how to write CSV properly. – Peter Duniho May 19 '21 at 07:28
  • You are missing four tabs. The output you want is putting data in the 5th column. What you are getting is data in column one. – jdweng May 19 '21 at 07:28

0 Answers0