Edza
Edza

Reputation: 1444

How do I convert a string like "\\u0012" into a Char "\u0012"

The string is literary "\\u0012" (example). I have to make it into a "\u0012" (notice the \ and \\). Char.Parse() doesn't work.

There must be a simple way. Perhaps try to convert the 0012 into a byte array and then somehow convert that into a char...

Upvotes: 0

Views: 3680

Answers (2)

corylulu
corylulu

Reputation: 3499

Are you just looking to replace the "\" with "\"? Or are you trying to create a char(12)?

I think what you are trying to do is

string originalString = Regex.Unescape("\\u0012");
char newChar = Char.Parse(originalString);

As mentioned above, this works. Tested. Just make sure to add

using System.Text.RegularExpressions;

Upvotes: 1

Oded
Oded

Reputation: 499172

You can use Regex.Unescape.

string unescaped = Regex.Unescape(mystring);

Upvotes: 3

Related Questions