How to remove escape sequences from stream

前端 未结 5 1406
后悔当初
后悔当初 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.

    0 讨论(0)
  • 2021-01-14 16:00

    Hope bellow syntax will be help full for you

    string inputString = @"hello world]\ ";
    
    StringBuilder sb = new StringBuilder();
    string[] parts = inputString.Split(new char[] { ' ', '\n', '\t', '\r', '\f', '\v','\\' }, StringSplitOptions.RemoveEmptyEntries);
    int size = parts.Length;
    for (int i = 0; i < size; i++)
        sb.AppendFormat("{0} ", parts[i]);
    
    0 讨论(0)
  • 2021-01-14 16:00

    Escape sequense is a string of characters usually beginning with ESC-char but can contain any character. They are used on terminals to control cursor position graphics-mode etc. http://en.wikipedia.org/wiki/Escape_sequence Here is my implement with python. Should be easy enough to translate to C.

    #!/usr/bin/python2.6/python
    import sys
    
    Estart="\033" #possible escape start keys
    Estop="HfABCDsuJKmhlp" #possible esc end keys
    replace="\015" # ^M character
    replace_with="\n"
    f_in = sys.stdin
    parsed = sys.stdout
    seqfile= open('sequences','w')#for debug
    
    
    in_seq = 0
    
    c = f_in.read(1)
    
    while len(c) > 0 and not c=='\0':
        while len(c)>0 and c!='\0' and not c in Estart:
            if not c in replace : 
                parsed.write(c)
            else:
                parsed.write(replace_with[replace.find(c)])
            c = f_in.read(1)
        while len(c)>0 and c!='\0' and not c in Estop:
            seqfile.write(c)
            c = f_in.read(1)
        seqfile.write(c) #write final character
        c = f_in.read(1)
    
    f_in.close()
    parsed.close()
    seqfile.close()
    
    0 讨论(0)
  • 2021-01-14 16:11

    You can use System.Char.IsControl() to detect control characters.

    To filter control characters from a string:

    public string RemoveControlCharacters(string input)
    {
        return
            input.Where(character => !char.IsControl(character))
            .Aggregate(new StringBuilder(), (builder, character) => builder.Append(character))
            .ToString();
    }
    

    To filter control characters from a stream you can do something similar, however you will first need a way to convert a Stream to an IEnumerable<char>.

    public IEnumerable<char> _ReadCharacters(Stream input)
    {
        using(var reader = new StreamReader(input))
        {
            while(!reader.EndOfStream)
            {
                foreach(var character in reader.ReadLine())
                {
                    yield return character;
                }
            }
        }
    }
    

    Then you can use this method to filter control characters:

    public string RemoveControlCharacters(Stream input)
    {
        return
            _ReadCharacters(input)
            .Where( character => !Char.IsControl(character))
            .Aggregate( new StringBuilder(), ( builder, character ) => builder.Append( character ) )
            .ToString();
    }
    
    0 讨论(0)
  • 2021-01-14 16:17

    If you're going for fewer lines of code:

    string inputString = "\ncheese\a";
    char[] escapeChars = new[]{ '\n', '\a', '\r' }; // etc
    
    string cleanedString = new string(inputString.Where(c => !escapeChars.Contains(c)).ToArray());
    
    0 讨论(0)
提交回复
热议问题