1

I have a string defined public const string Numeric0 = "\u0030";. C# is interpreting it as a Unicode character, which is useful to part of my application, but I also want to output the string in its original "\u0030" format. I've tried most related answers here from SO, but haven't really made any progress. Thoughts?

Expected c# input:

public const string Numeric0 = "\u0030";
var str = SomeOperation(Numeric0);

Expected output:

str == @"\u0030"
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Drise
  • 4,310
  • 5
  • 41
  • 66
  • 2
    You want the string to *literally* contain `"\u0030"`? –  Jul 30 '19 at 14:40
  • 3
    Could you show us what you've tried and what the result was? It's expect `$@"\u{(int) ch:x4}"` to work (for a `char ch`). Note that if you need to handle surrogate pairs (formatting as `\Uxxxxxxxx`) that would be slightly trickier. – Jon Skeet Jul 30 '19 at 14:40
  • Yes, literally "\u0030" – Drise Jul 30 '19 at 14:42
  • 2
    So... `"\\u0030"` or `@"\u0030"`? Or if you want the string to contain the unicode character 0030, but still want to print it out as `\u0030`, see @JonSkeet's comment – canton7 Jul 30 '19 at 14:43
  • Possible duplicate of [converting unicode to actual character C#](https://stackoverflow.com/questions/36611182/converting-unicode-to-actual-character-c-sharp) – John Smith Jul 30 '19 at 14:43
  • I've tried various `Regex.Escape` and the one @JohnSmith linked. That is how to convert to the actual represented character, not get the escaped unicode string. – Drise Jul 30 '19 at 14:45
  • @Drise I think you need to add some expected input and output. At the moment it's not at all clear what you want, and why the other answers on here don't apply to you – canton7 Jul 30 '19 at 14:45
  • @canton7 Edited. – Drise Jul 30 '19 at 14:49
  • See @JonSkeet's comment - that should do what you want. – canton7 Jul 30 '19 at 14:50
  • Your "expected output" would be just for an operation that returns its input. I assume you *actually* want `@"\u0030"`. It's also not clear what you want to happen if there are multiple characters. – Jon Skeet Jul 30 '19 at 14:52
  • @JonSkeet Multiple characters are not part of a valid input. Sorry for not clarifying. Is there a cleaner way to do `var x = $@"\u{(int)Numeric0.ToCharArray()[0]:x4}";`, which is doing what I want? – Drise Jul 30 '19 at 14:54
  • 1
    `$@"\u{(int)Numeric0[0]:x4}"` -- no need for `ToCharArray()`. Also, why not declare your const as a `char`, rather than a `string`, if it will only contain a single character? – canton7 Jul 30 '19 at 14:56
  • @canton7 Derp... Also, the tool I originally used to grab all these values gave them to me as strings. Didn't think about it. – Drise Jul 30 '19 at 14:57

1 Answers1

2

So you want a kind of string dump (when each char is represented in a form of unicode \uxxxx symbol), let's implement it with a help of Linq:

using System.Linq;

...

public static string Dump(string value) => value == null
  ? "null" //TODO: put desired representation of null string here
  : "\"" + string.Concat(value.Select(c => $"\\u{((int)c):x4}")) + "\"";

...

public const string Numeric0 = "\u0030";

...

string str = Dump(Numeric0);

Console.WriteLine(str);
Console.WriteLine(Dump("abc 123"));

Outcome:

"\u0030"
"\u0061\u0062\u0063\u0020\u0031\u0032\u0033"
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215