4

I have string for example:

string str = "ĄĆŹ - ćwrą";

How can i replace ĄĆŹ - ćą with they shortcuts? Result for that example string should be:

str = "\u0104\u0106\u0179 \u2013 \u0107wr\u0105"

Is there any fast replacement method? I dont want to use .Replace for each character...

3
  • Replace does strings.. Commented Jun 23, 2013 at 13:43
  • possible duplicate of Converting Unicode strings to escaped ascii string Commented Jun 23, 2013 at 13:44
  • Thanks, i also just found it. Problem solved. @Sayse i don't want to use .Replace hundreds of times Commented Jun 23, 2013 at 13:51

1 Answer 1

8

Converting to a JSON string like that is more cumbersome than it should be, mainly because you need to work with Unicode code points which in practice means calling char.ConvertToUtf32. In order to do that, you need to somehow handle surrogate pairs; System.Globalization.StringInfo can help with that.

Here's a function that uses these building blocks to perform the conversion:

string str = "ĄĆŹ - ćwrą";

public string ToJsonString(string s)
{
    var enumerator = StringInfo.GetTextElementEnumerator(s);
    var sb = new StringBuilder();

    while (enumerator.MoveNext())
    {
        var unicodeChar = enumerator.GetTextElement();
        var codePoint = char.ConvertToUtf32(unicodeChar, 0);
        if (codePoint < 0x80) {
            sb.Append(unicodeChar);
        }
        else if (codePoint < 0xffff) {
            sb.Append("\\u").Append(codePoint.ToString("x4"));
        }
        else {
            sb.Append("\\u").Append((codePoint & 0xffff).ToString("x4"));
            sb.Append("\\u").Append(((codePoint >> 16) & 0xffff).ToString("x4"));
        }
    }

    return sb.ToString();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.