Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode literal string

I'm sending some JSON in an HTTP POST request. Some of the text within the JSON object is supposed to have superscripts.

If I create my string in C# like this:

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 exactly as I write it in the the string, not as a superscript.

If I add an escape character, then it shows up like: "here is my superscript: \\u00B9"

I don't want to use an escape character, but I also don't want it to be converted to the actual superscript. Is there a way to have C# not do Unicode conversion and leave it as literally: "\u00B9"?

like image 563
user1373121 Avatar asked Jun 24 '13 16:06

user1373121


People also ask

Which string literals are Unicode strings in Python?

A Python 2 Unicode literal ( u'some text' ) is a different type of Python object from a python byte string ( 'some text' ). It's like using \t versus \T ; the former has meaning in python literals (it's interpreted as a tab character), the latter just means a backslash and a capital T (two characters).

What is Unicode string in Python?

To summarize the previous section: a Unicode string is a sequence of code points, which are numbers from 0 through 0x10FFFF (1,114,111 decimal). This sequence of code points needs to be represented in memory as a set of code units, and code units are then mapped to 8-bit bytes.

What is a literal string give an example?

A string literal is a sequence of zero or more characters enclosed within single quotation marks. The following are examples of string literals: 'Hello, world!' '10-NOV-91' 'He said, "Take it or leave it."'

What is literal string?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.


3 Answers

If I understand your question correctly... add the at symbol (@) before the string to avoid the escape sequences being processed

 string s = @"here is my superscript: \u00B9";

http://msdn.microsoft.com/en-us/library/362314fe(v=vs.80).aspx

like image 122
NinjaNye Avatar answered Sep 23 '22 12:09

NinjaNye


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"

like image 30
Adrian Ratnapala Avatar answered Sep 26 '22 12:09

Adrian Ratnapala


is recommended you encode your string before send to server. You can encode using base64 or URLEncode in client and decode in server side.

like image 34
Wilker Iceri Avatar answered Sep 23 '22 12:09

Wilker Iceri