简体   繁体   English

C#读取文件添加特殊字符

[英]C# Reading a file add special characters

I am reading a text file with this structure: 我正在读取具有以下结构的文本文件:

20150218;"C7";"B895";00101;"FTBCCAL16" 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: 但是,当我看到“ linea”的内容时,我有以下内容:

"20150218";\\"C7\\";\\"B895\\";"00101";\\"FTBCCAL16\\" “ 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" 20150218;“ C7”;“ B895”; 00101;“ FTBCCAL16”

Is there a way to obtain this? 有没有办法做到这一点? Thanks in advance! 提前致谢! Regards! 问候!

You are watching it in Visual Studio debugger, which just shows you your lines this way. 您正在Visual Studio调试器中观看它,它只是以这种方式向您显示行。 You can write your result into a console or into the file. 您可以将结果写入控制台或文件中。 And you will see normal text without special characters. 您会看到没有特殊字符的普通文本。

StreamReader is not adding or modifying the strings read from the file at all. StreamReader根本不添加或修改从文件读取的字符串。

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). 如果要在Visual Studio调试器中查看separador的内容,它将为任何特殊字符添加一个转义序列(出于显示目的)。

The displayed format matches how you would have to enter them in the code editor if you were creating a string constant. 如果创建一个字符串常量,则显示的格式与您在代码编辑器中输入它们的方式匹配。

For example, 例如, 调试器输出


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. 如果输出它们或尝试用代码对其进行操作,它们将具有正确的内容。

控制台输出


So, your code is correct. 因此,您的代码是正确的。 You just have to understand escape sequences and how strings appear in the Visual Studio debugger. 您只需要了解转义序列以及字符串在Visual Studio调试器中的显示方式即可。


Update: 更新:

See this question for an explanation of how to display unquoted strings in the debugger. 这个问题对于如何显示在调试器中未加引号的字符串的解释。

Okay here is the quotation from MSDN 好的,这是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". 例如,逐字字符串@“ C:\\ files.txt”在监视窗口中将显示为“ C:\\ files.txt”。

In your case for " it uses \\" ( Verbatim string ) and this can be visible at debugging time. 在您的情况下, "它使用\\" ( Verbatim字符串) ,可以在调试时看到。

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 . 因此,当字符串有目的地包含转义序列时,您需要将其表示为verbatim string That's what compiler do and that's what you see in debugger 那就是编译器所做的,这就是您在调试器中看到的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM