转义在字符串中的双引号在C#

转义在字符串中的双引号在C#

本文介绍了转义在字符串中的双引号在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经涵盖了很多次,但我仍然有一个问题,所有的解决方案。

我需要建立一个字符串发送到需要的报价中有一个JSON解析器。我已经试过这些形式:

 字符串T1 =[{\TS \}];
串T2 =[{+\+TS+\+}];
字符串T3 = @[{,TS}];
Debug.Print(T1);
Debug.Print(T1);
Debug.Print(T1);
 

调试语句正确地显示它[{TS}],但是当我看到它在调试器中,最重要的,当我发送字符串到我的服务器端的JSON解析器中有转义字符:[{\TS \}]

我怎样才能在实际的字符串摆脱的转义字符?

解决方案

从看它会始终显示转义版本的调试点(这是所以你作为开发人员,知道该字符串值是什么)。这不是错误。当你将其发送给其他.NET系统,它会再次显示但从调试点转义版本。如果输出值,(回复于()或Console.WriteLine()),您将看到您所期望的版本将在那里。

如果您选中的变量(从调试器),然后选择旁边的放大镜图标下拉列表,选择文本展示台,你会看到它以纯文本的显示方式。这可能是你所期待的。

每您的意见,我想建议你还看你如何转换你的字符串中的字节。你要确保你的连接code的格式的字节可以被其他机器所理解。请务必将您的字符串转换成使用命令字节如下:

  System.Text.Encoding.ASCII.GetBytes(MyString的);
 

我有偷偷怀疑您发送重新字符串本身,而不是一间codeD版presentation位。

I know this has been covered lots of times but I still have a problem with all of the solutions.

I need to build a string to send to a JSON parser which needs quotes in it. I've tried these forms:

string t1 = "[{\"TS\"}]";
string t2 = "[{" + "\"" + "TS" + "\"" + "}]";
string t3 = @"[{""TS""}]";
Debug.Print(t1);
Debug.Print(t1);
Debug.Print(t1);

The debug statement shows it correctly [{"TS"}] but when I look at it in the debugger and most importantly when I send the string to my server side json parser is has the escape character in it:"[{\"TS\"}]"

How can I get rid of the escape characters in the actual string?

解决方案

From the debugger point of view it will always show the escaped version (this is so you, as the developer, know exactly what the string value is). This is not an error. When you send it to another .Net system, it will again show the escaped version from the debugger point of view. If you output the value, (Response.Write() or Console.WriteLine()) you will see that the version you expect will be there.

If you highlight the variable (from the debugger) and select the dropdown next to the magnifying glass icon and select "Text Visualizer" you will see how it displays in plain text. This may be what you are looking for.

Per your comments, i wanted to suggest that you also watch how you convert your string in to bytes. You want to make sure you encode your bytes in a format that can be understood by other machines. Make sure you convert your string into bytes using a command as follows:

System.Text.Encoding.ASCII.GetBytes(mystring);

I have the sneaking suspicion that you are sending the bit representation of the string itself instead of an encoded version.

这篇关于转义在字符串中的双引号在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:39