Convert string to char

c#

Solution

New line:

string escapedNewline = @"\\n";
string cleanupNewLine = escapedNewline.Replace(@"\\n", Environment.NewLine);

OR

string cleanupNewLine = escapedNewline.Replace(@"\\n", "\n");

Tab:

string escapedTab = @"\\t";
string cleanupTab= escapedTab.Replace(@"\\t", "\t");

Note the lack of the literal string (i.e. i did not use @"\t" because that will not represent a Tab)

Alternatively you could consider Regular Expressions if you need to replace a range of different string patterns.

You should probably write a utility function to encapsulate the common behaviour above for all the possible Escape Sequences

Then you'd write some Unit Tests to cover each of the cases you can think of.

As you encounter any bugs you add more unit tests to cover those cases.

UPDATE

You could represent a tab in the XML with a special character sequence:

`	` see this article

This article applies to SQL Server but may well be relevant to C# also?

To be absolutely sure, you could try generating a string with a tab in it and putting it into some XML (programmatically) and using XmlSerializer to serialize that to a file to see what the output is, then you can be sure that this will faithfully 'round-trip' the string with the tab still in it.

Problem

I get from another class string that must be converted to char. It usually contains only one char and that's not a problem. But control chars i receive like '\\n' or '\\t'. Is there standard methods to convert this to endline or tab char or i need to parse it myself? edit: Sorry, parser eat one slash. I receive '\\t'

Original source