C# Unicode 文字串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17280482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unicode literal string
提问by user1373121
im sending some json in a http post request. some of the text within the json object is supposed to have superscripts.
我在 http post 请求中发送了一些 json。json 对象中的一些文本应该有上标。
if i create my string in C# like:
如果我在 C# 中创建我的字符串,例如:
string s = "here is my superscript: \u00B9";
it converts the \u00B9 to the actual superscript 1, which breaks my json. I want the \u00B9 to show up just as I write in the the string, not as a superscript.
它将 \u00B9 转换为实际的上标 1,这破坏了我的 json。我希望 \u00B9 像我在字符串中写的那样显示,而不是作为上标。
If I add an escape character, then it shows up like: "here is my superscript: \\u00B9"
如果我添加一个转义字符,那么它会显示为:“这是我的上标:\\u00B9”
I dont want to use an escape character, but I also dont want it to be converted to the actual superscript. Is there a way to have C# not do unicode conversion and leave it as litterally: "\u00B9"?
我不想使用转义字符,但我也不希望将其转换为实际的上标。有没有办法让 C# 不进行 unicode 转换并将其保留为:“\u00B9”?
Thanks in advance
提前致谢
回答by Wilker Iceri
is recommended you encode your string before send to server. You can encode using base64 or URLEncode in client and decode in server side.
建议您在发送到服务器之前对字符串进行编码。您可以在客户端使用 base64 或 URLEncode 进行编码并在服务器端进行解码。
回答by NinjaNye
If I understand your question correctly... add the at symbol (@) before the string to avoid the escape sequences being processed
如果我正确理解您的问题...在字符串前添加 at 符号 (@) 以避免处理转义序列
string s = @"here is my superscript: \u00B9";
http://msdn.microsoft.com/en-us/library/362314fe(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/362314fe(v=vs.80).aspx
回答by Adrian Ratnapala
I like @NinjaNye's answer, but the other approach is to use a double-backslash to make it literal. Thus string s = "here is my superscript: \\u00B9"
我喜欢@NinjaNye 的回答,但另一种方法是使用双反斜杠使其成为文字。因此string s = "here is my superscript: \\u00B9"

