0

I tried to find how to ignore escape characters like \n in a string (example string: Console.WriteLine("how\n are you\n");) I found how to ignore case sensitive mostly, but those are not escape characters like \n. (On phone I don't have an example of the whole code. Will update when I can reach laptop)

So, how do I ignore if disable escape characters in C#?

(EDIT) Thanks for the help, wasn't aware they were called escape commands. Yes I wanted to show them as characters, sorry for the vague question.

Odin1999
  • 11
  • 6
  • 5
    `\n` is not an inline command, its an [escape character](https://blogs.msdn.microsoft.com/csharpfaq/2004/03/12/what-character-escape-sequences-are-available/). Are you trying to strip out all _escape characters_? – maccettura Jan 31 '18 at 17:31
  • 1
    These aren't inline commands. They are escape sequences that represent *characters* like newlines, tabs, double quotes etc. It's not clear what you want to do either. Do you want to replace some of those *characters*? Or type a string literal where `\n ` is interpreted as two characters instead of a single newline? – Panagiotis Kanavos Jan 31 '18 at 17:32
  • Do you need to remove any other characters than `\n`,`\r`,`\r\n`? If no, then just do `var bar = foo.Replace(@"\n", "").Replace(@"\r", "")` – FCin Jan 31 '18 at 17:32
  • https://stackoverflow.com/questions/15748040/how-do-i-write-the-escape-char-to-code – Slai Jan 31 '18 at 17:53

1 Answers1

4

This could be a solution:

string a = "how\n are you\n"
a = a.Replace("\n", string.Empty);
Console.WriteLine(a);

Idea is always the same, you have to replace such a constractions like \n, \r etc with empty string. More advanced ways of how to do that you could find here: How to remove new line characters from a string? or here: Replace Line Breaks in a String C#.

On demand of Yollo here is a one line version:

 Console.WriteLine("how\n are you\n".Replace("\n", string.Empty));
Alex
  • 790
  • 7
  • 20