How to remove escape sequences from stream

前端 未结 5 1411
后悔当初
后悔当初 2021-01-14 15:48

is there an quick way to find(and remove) all escape sequences from a Stream/String??

5条回答
  •  迷失自我
    2021-01-14 15:50

    The escape sequences that you are referring to are simply text based represntations of characters that are normally either unprintable (such as new lines or tabs) or conflict with other characters used in source code files (such as the backslash "\").

    Although when debugging you might see these chracters represented as escaped characters in the debugger, the actual characters in the stream are not "escaped", they are those actual characters (for example a new line character).

    If you want to remove certain characters (such as newline characters) then remove them in the same way you would any other character (e.g. the letter "a")

    // Removes all newline characters in a string
    myString.Replace("\n", "");
    

    If you are actually doing some processing on a string that contains escaped characters (such as a source code file) then you can simply replace the escaped string with its unescaped equivalent:

    // Replaces the string "\n" with the newline character
    myString.Replace("\\n", "\n");
    

    In the above I use the escape sequence for the backslash so that I match the string "\n", instead of the newline character.

提交回复
热议问题