No se puede reemplazar un salto de línea en particular de un archivo delimitado por tabuladores MYOB

I have a tab delimited file, and in one particular field, sometimes the content will contain a sentence with a line feed character in the middle of it after looking at it on Notepad++. Subsequently, when the program tries to split this line with the delimiter, it sort of stops at that point and starts again which is bad.

So I've been doing to usual with replace, and then trim to get rid of it, but it's not picked them up. i.e.

line = line.Replace("\r", " ").Replace("\n", " ");


line = line.Trim('\r', '\n');

What am I missing? Is there another representation of \n out there?

Edit. I have also tried (char)10 and didn't find it either.

Edit 2. As a big picture, I've solved what I needed to do, but not with this particular method of replacing. Because I was using .Readline() on my file, I determined replace wouldn't work regardless as that line had finished even though I know it wasn't, so I would read into the next line and then combine the two strings together and my mystery line feed was gone.

preguntado el 27 de noviembre de 13 a las 06:11

did you turn wordwrap off in notepad ++ -

Just turned it off, and the emptiness after the line feed is even more obvious now. haha. -

'trim' just removes those characters from the start and end, so I'd stick with replace. Did you do all three together? line = line.Replace("\r", " ").Replace("\n", " ").Replace((char)10, " ");? -

the other option would be to verify the column type is varchar or nvarchar, verify it is pulled out as the type it is, then you can do an htmlencode of the value (or other encoding technique) to verify the ascii values that are causing the whitespace. -

I tried line = line.Replace("\r", " ").Replace("\n", " ").Replace("(char)10", " "); and no luck. When (char)10 is not wrapped in quote marks, the line is shown as an error. -

1 Respuestas

De los comentarios a la pregunta:

You mentioned you tried (char)10, but it only worked if it was in quotes. That just treats it as "(char)10" (a string) instead of the character that a base 10 (decimal) value of "10" actually is: a linefeed character.

It didn't work without quotes because replacing a char requires that the char be replaced with a char instead of a string, which is what happens by default when we don't specify a string is actually a char. (confusing, I know). In VB, and perhaps in C# as well, this is denoted with a c after the closing quote.


line = line.Replace("\r", " ").Replace("\n", " ").Replace((char)10, " "c);

The other possible way of doing this without using the " "c is to use the char of the space's ascii value (32):

line = line.Replace("\r", " ").Replace("\n", " ").Replace((char)10, (char)32);

That's probably why your initial attempt didn't work when trying to replace the linefeed character.

I think you also may have been fine if you replaced the \n by itself (especially before replacing the \r), or Environment.NewLine, which represents the CRLF - control return (\r) line feed ((char)10) used to denote a new line in windows. (Environment.NewLine is able to adapt to the system the code is running from, though, whereas \n is always CRLF... Correct me if I'm wrong.)

respondido 27 nov., 13:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.