在RichTextBox中格式化rtf / unicode / utf-8的最简单方法?

时间:2022-10-30 12:29:15

I'm currently beating my head against a wall trying to figure this out. But long story short, I'd like to convert a string between 2 UTF-8 '\u0002' to bold formating. This is for an IRC client that I'm working on so I've been running into these quite a bit. I've treid regex and found that matching on the rtf as ((\'02) works to catch it, but I'm not sure how to match the last character and change it to \bclear or whatever the rtf formating close is.

我正在试图解决这个问题。但长话短说,我想将2 UTF-8'\ u0002'之间的字符串转换为粗体格式。这是我正在研究的IRC客户端所以我已经遇到了很多这样的问题。我已经treid正则表达式并发现匹配rtf为((\'02)可以捕获它,但我不确定如何匹配最后一个字符并将其更改为\ bclear或者任何rtf格式化close。

I can't exactly paste the text I'm trying to parse because the characters get filtered out of the post. But when looking at the char value its an int of 2.

我无法准确粘贴我正在尝试解析的文本,因为字符会从帖子中过滤掉。但是当查看char值时,它的int为2。

Here's an attempt to paste the offending text:

这是尝试粘贴有问题的文本:

[02:34] test test

[02:34]测试

2 个解决方案

#1


You could use either

你也可以使用

rtb.Rtf = Regex.Replace(rtb.Rtf, @"\\'02\s*(.*?)\s*\\'02", @"\b $1 \b0");

or

rtb.Rtf = Regex.Replace(rtb.Rtf, @"\\'02\s*(.*?)\s*\\'02", @"\'02 \b $1 \b0 \'02");

depending on whether you want to keep the \u0002s in there.

取决于你是否想要保留\ u0002s。

The \b and \b0 turn the bold on and off in RTF.

\ b和\ b0在RTF中打开和关闭粗体。

#2


I don't have a test case, but you could also probably use the Clipboard class's GetText method with the Unicode TextDataFormat. Basically, I think you could place the input in the clipboard and get it out in a different format (works for RTF and the like). Here's MS's demo code (not applicable directly, but demonstrates the API):

我没有测试用例,但您也可以使用Clipboard类的GetText方法和Unicode TextDataFormat。基本上,我认为您可以将输入放在剪贴板中并以不同的格式将其输出(适用于RTF等)。这是MS的演示代码(不直接适用,但演示了API):

// Demonstrates SetText, ContainsText, and GetText. 
public String SwapClipboardHtmlText(String replacementHtmlText)
{
    String returnHtmlText = null;
    if (Clipboard.ContainsText(TextDataFormat.Html))
    {
        returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
        Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
    }
    return returnHtmlText;
}

Of course, if you do that, you probably want to save and restore what was in the clipboard, or else you may upset your users!

当然,如果你这样做,你可能想要保存和恢复剪贴板中的内容,否则你可能会让用户感到不安!

#1


You could use either

你也可以使用

rtb.Rtf = Regex.Replace(rtb.Rtf, @"\\'02\s*(.*?)\s*\\'02", @"\b $1 \b0");

or

rtb.Rtf = Regex.Replace(rtb.Rtf, @"\\'02\s*(.*?)\s*\\'02", @"\'02 \b $1 \b0 \'02");

depending on whether you want to keep the \u0002s in there.

取决于你是否想要保留\ u0002s。

The \b and \b0 turn the bold on and off in RTF.

\ b和\ b0在RTF中打开和关闭粗体。

#2


I don't have a test case, but you could also probably use the Clipboard class's GetText method with the Unicode TextDataFormat. Basically, I think you could place the input in the clipboard and get it out in a different format (works for RTF and the like). Here's MS's demo code (not applicable directly, but demonstrates the API):

我没有测试用例,但您也可以使用Clipboard类的GetText方法和Unicode TextDataFormat。基本上,我认为您可以将输入放在剪贴板中并以不同的格式将其输出(适用于RTF等)。这是MS的演示代码(不直接适用,但演示了API):

// Demonstrates SetText, ContainsText, and GetText. 
public String SwapClipboardHtmlText(String replacementHtmlText)
{
    String returnHtmlText = null;
    if (Clipboard.ContainsText(TextDataFormat.Html))
    {
        returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
        Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
    }
    return returnHtmlText;
}

Of course, if you do that, you probably want to save and restore what was in the clipboard, or else you may upset your users!

当然,如果你这样做,你可能想要保存和恢复剪贴板中的内容,否则你可能会让用户感到不安!