vb.net 使用VB将带有RTF格式的字符串转换为纯文本字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31430811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:17:17  来源:igfitidea点击:

Convert a string with RTF Format in it to a plain text string using VB

vb.net

提问by milheiros

This is my first project in VB.Net I have a string with RTF format. I need to delete/remove the group of bullets when it appears in that string, using VB.Net.

这是我在 VB.Net 中的第一个项目,我有一个 RTF 格式的字符串。当它出现在该字符串中时,我需要使用 VB.Net 删除/移除项目符号组。

My string:

我的字符串:

{\rtf1\ansi\ansicpg1252\deff0\deflang2070{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}{\f1\fnil\fcharset0 Tahoma;}{\f2\fnil\fcharset2 Symbol;}}
\viewkind4\uc1\pard\f0\fs17\par
\pard{\pntext\f2\'B7\tab}{\*\pn\pnlvlblt\pnf2\pnindent0{\pntxtb\'B7}}\f1 one\par
{\pntext\f2\'B7\tab}two\par
{\pntext\f2\'B7\tab}three\par
{\pntext\f2\'B7\tab}\par
\pard\fs17\par
}

I have 3 bullets called one, two, three. I need to remove the format but maintain the values each in line.

我有 3 颗子弹,分别称为一、二、三。我需要删除格式但保留每个值。

I've tried something like this in VB

我在 VB 中尝试过这样的事情

If (Rtfctrl.FindNumberOfOccurences(Txt, "\bullet", True) = 1) Then
     resultString = resultString.Replace("\bullet", "")
End If
}

... but bullet is not in the string. The list bullets are a group with a more complex sintax.

...但子弹不在字符串中。列表项目符号是一组具有更复杂语法的组。

Who can I do that? Regards, Filipe

我谁能做到?问候, 菲利普

采纳答案by Blackwood

I wouldn't usually suggest using a Control for something other than user interaction, but a RichTextBox can convert RTF text into plain text. You can create a RichTextBox without ever adding it to the Form. Then assign the RTF text to the Rtf property and read the plain text from the Text or Lines property. For example:

我通常不建议将 Control 用于用户交互以外的其他用途,但 RichTextBox 可以将 RTF 文本转换为纯文本。您可以创建 RichTextBox,而无需将其添加到 Form。然后将 RTF 文本分配给 Rtf 属性并从 Text 或 Lines 属性读取纯文本。例如:

Dim rtf As String = "{\rtf1\ansi\ansicpg1252\deff0\deflang2070{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}" _
    & "{\f1\fnil\fcharset0 Tahoma;}{\f2\fnil\fcharset2 Symbol;}}\viewkind4\uc1\pard\f0\fs17\par" _
    & "\pard{\pntext\f2\'B7\tab}{\*\pn\pnlvlblt\pnf2\pnindent0{\pntxtb\'B7}}\f1 one\par{\pntext\f2\'B7\tab}two\par" _
    & "{\pntext\f2\'B7\tab}three\par{\pntext\f2\'B7\tab}\par\pard\fs17\par}"
Dim rBox As New RichTextBox
rBox.Rtf = rtf
Dim txt() As String = rBox.Lines

After running this code, txt(1) contains "one", txt(2) contains "two" and txt(3) contains "three".

运行这段代码后,txt(1) 包含“一”,txt(2) 包含“二”,txt(3) 包含“三”。