Patrick
Patrick

Reputation: 419

Convert backslash-escaped characters to literals, within a string

Are there any .NET provided functions to convert a string with backslash-escaped characters into the literal string?
For example, the string @"this\x20is a\ntest" should become "this is a\ntest", where \n is a literal newline character and \x20 is a literal space. These would (preferably) be Microsoft escape characters.

Upvotes: 3

Views: 361

Answers (1)

The One
The One

Reputation: 4784

Try using Regex.Unescape

using System.Text.RegularExpressions;
...

string result=Regex.Unescape(@"this\x20is a\ntest");

This results in:

this is a 
test

https://dotnetfiddle.net/y2f5GE

It might not work all the time as expected, please read the docs for details

Upvotes: 3

Related Questions