0

I had to replace all character occurrence in string. For example:

string a = "202F";
string b = $"\u{a}";  # There is an error - unrecognized escape character

Regex pattern = new Regex($"[{b}]");
n = pattern.Replace(n, "_");

How to make UTF-8 code from variables?

Barburka
  • 407
  • 1
  • 7
  • 15
  • use the [`char`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/char) type – DekuDesu May 22 '21 at 18:00
  • Thanks, I consider that but is there any solution to merge "\u" + "202F" in char variable? I had that ending code in my DB, maybe I should storage all char code for example: '\u202F' – Barburka May 22 '21 at 18:02
  • 2
    Use `$"\\u{a}"` or `$@"\u{a}"`. – 41686d6564 stands w. Palestine May 22 '21 at 18:03
  • 1
    The `\u` syntax can be used only with _literals_. Once the code is compiled, there's no facility to interpret string escapes, including `\u`. If your first string contains the hexadecimal representation of a Unicode code point (**note**: you do _not_ have UTF-8 here), then you simply need to parse the value to get the actual integer value, and then just cast that to `char`. E.g. `char b = (char)int.Parse(a, NumberStyles.HexNumber);`. See e.g. duplicate. – Peter Duniho May 22 '21 at 18:08
  • "The \u syntax can be used only with literals". This is the answer I've looked for – Barburka May 22 '21 at 18:20

0 Answers0