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

发布于 2024-07-13 13:42:44 字数 334 浏览 5 评论 0原文

我目前正在把头撞在墙上试图解决这个问题。 但长话短说,我想将 2 个 UTF-8 '\u0002' 之间的字符串转换为粗体格式。 这是针对我正在开发的 IRC 客户端的,所以我经常遇到这些问题。 我有 treid 正则表达式,发现 rtf 上的匹配为 ((\'02) 可以捕获它,但我不确定如何匹配最后一个字符并将其更改为 \bclear 或任何 rtf 格式化关闭。

我无法准确粘贴我试图解析的文本,因为字符被从帖子中过滤掉了,但是当查看 char 值时,它的整数为 2。

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

[02:34]测试测试

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.

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.

Here's an attempt to paste the offending text:

[02:34] test test

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

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

发布评论

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

评论(2

隱形的亼 2024-07-20 13:42:44

您可以使用 或

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

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

具体取决于您是否想将 \u0002 保留在那里。

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

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.

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

我是男神闪亮亮 2024-07-20 13:42:44

我没有测试用例,但您也可以使用 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;
}

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

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):

// 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!

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