Home > Blockchain >  Remove escape sequence from string in c#
Remove escape sequence from string in c#

Time:07-22

I'm facing an issue with removing escape sequence from string using C#

var street = "1324 W. 650 N.\t";

Above I mentioned my code please check once and mention below comments on how to remove escape sequences like "\t".

CodePudding user response:

If you only want to remove '\t' try:

var street = "1324 W. 650 N.\t";
var removed = street.Replace("\t", "");

In case you would also like to remove '\n' and '\r' too, you can concatenate the operation 'Replace()'.

var street = "1324 W. 650 N.\t";
var removed = street.Replace("\t", "").Replace("\n", "").Replace("\r", "");

Check the example I made.

CodePudding user response:

You'll want to use Regex.Unescape method.

String unescapedString = Regex.Unescape(textString);

However, becareful that the Regex.Unescape doesn't un-escape ". According to the documentation, it does the following:

..by removing the escape character ("") from each character escaped by the method. These include the , *, , ?, |, {, [, (,), ^, $,., #, and white space characters. In addition, the Unescape method unescapes the closing bracket (]) and closing brace (}) characters.

  • Related