如何从流中删除转义序列

发布于 2024-12-03 13:56:51 字数 121 浏览 2 评论 0原文

有没有一种快速的方法可以找到(并删除)所有转义序列流/字符串?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

平生欢 2024-12-10 13:56:51

希望以下语法对您有所帮助

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]);

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]);
稀香 2024-12-10 13:56:51

您所指的转义序列只是基于文本的字符表示,这些字符通常不可打印(例如换行符或制表符)或与源代码文件中使用的其他字符冲突(例如反斜杠“\”)。

尽管在调试时您可能会看到这些字符在调试器中表示为转义字符,但流中的实际字符并未“转义”,它们是那些实际字符(例如换行符)。

如果您想删除某些字符(例如换行符),请按照与删除任何其他字符(例如字母“a”)相同的方式删除它们

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

如果您实际上正在对包含转义字符的字符串进行某些处理(例如源代码文件),那么您可以简单地将转义字符串替换为其未转义的等效字符串:

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

在上面,我使用反斜杠的转义序列,以便匹配字符串“\n”,而不是换行符。

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.

╰沐子 2024-12-10 13:56:51

如果您想要更少的代码行:

string inputString = "\ncheese\a";
char[] escapeChars = new[]{ '\n', '\a', '\r' }; // etc

string cleanedString = new string(inputString.Where(c => !escapeChars.Contains(c)).ToArray());

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());
长亭外,古道边 2024-12-10 13:56:51

您可以使用 System.Char.IsControl() 来检测控制字符。

从字符串中过滤控制字符:

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

要从流中过滤控制字符,您可以执行类似的操作,但是您首先需要一种将 Stream 转换为 IEnumerableIEnumerable的方法。代码>.

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

那么就可以使用这个方法来过滤控制字符:

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

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();
}
神经大条 2024-12-10 13:56:51

转义序列是一个字符串,通常以 ESC-char 开头,但可以包含任何字符。它们在终端上用于控制光标位置图形模式等。
http://en.wikipedia.org/wiki/Escape_sequence
这是我用 python 实现的。应该很容易翻译成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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文