Tarlan Ahadli Tarlan Ahadli - 3 years ago 159
C# Question

C# - Replace Chars with its Unicode instance

I'm developing the android application that reads book from JSON format.In order to create such type of books i needed the desktop application due to comfortableness and i chose C#.

First of all i want to say that in my native language there are lots of chars that should be encoded in Unicode not in ASCII for example...
[ə ç ş ğ ö ü and so on]

My problem is that there is problem with Json for some char formats and i should use the instance of this chars.(Unicode instance).For instance:

string text = "asdsdas";
text = ConvertToUnicode(Text);//->/u231/u213/u123...

i tried many ways to achieve this in JavaScript but i couldn't. Now devs please help me to solve this problem in C#.Thanks in advance any suggestion would be okay for me :).

Answer Source

You can define an extension method:

public static class Extension {
    public static string ToUnicodeString(this string str) {
        StringBuilder sb = new StringBuilder();
        foreach(var c in str) {
            sb.Append("\\u" + ((int) c).ToString("X4"));
        return sb.ToString();

which can be called like myString.ToUnicodeString()

Check it in this demo.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download