1

I am reading a text file with this structure:

20150218;"C7";"B895";00101;"FTBCCAL16"

I read the line and split like this:

System.IO.StreamReader fichero = new System.IO.StreamReader(ruta, Encoding.Default);
while ((linea = fichero.ReadLine()) != null)
{
    // Split by ";"
    String[] separador = linea.Split(';');
}

But when I see the content of "linea", I have this:

"20150218";\"C7\";\"B895\";"00101";\"FTBCCAL16\"

As you see, the streamreader add some special character to the output like "" and \. I want to obtain this.

20150218;"C7";"B895";00101;"FTBCCAL16"

Is there a way to obtain this? Thanks in advance! Regards!

Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
mcl
  • 97
  • 1
  • 11

3 Answers3

3

You are watching it in Visual Studio debugger, which just shows you your lines this way. You can write your result into a console or into the file. And you will see normal text without special characters.

EngineerSpock
  • 2,575
  • 4
  • 35
  • 57
2

StreamReader is not adding or modifying the strings read from the file at all.

If you are viewing the contents of separador in the Visual Studio debugger, it will add an escape sequence to any special characters (for display purposes).

The displayed format matches how you would have to enter them in the code editor if you were creating a string constant.

For example, Debugger output


However, the real contents of these strings (in memory) are not escaped. They are exactly as you expect them to be in your question.

If you output them or try to manipulate them in code they will have the correct contents.

Console output


So, your code is correct. You just have to understand escape sequences and how strings appear in the Visual Studio debugger.


Update:

See this question for an explanation of how to display unquoted strings in the debugger.

Community
  • 1
  • 1
Chris O'Neill
  • 1,742
  • 12
  • 14
0

Okay here is the quotation from MSDN

At compile time, verbatim strings are converted to ordinary strings with all the same escape sequences. Therefore, if you view a verbatim string in the debugger watch window, you will see the escape characters that were added by the compiler, not the verbatim version from your source code. For example, the verbatim string @"C:\files.txt" will appear in the watch window as "C:\files.txt".

In your case for " it uses \" (Verbatim string)and this can be visible at debugging time.

Why this happens ?

Double quotation mark " is an escape sequence

Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark (")

So when a string purposefully contains an escape sequence, you need to represent it as a verbatim string. That's what compiler do and that's what you see in debugger

Kavindu Dodanduwa
  • 12,193
  • 3
  • 33
  • 46