提取和转换

时间:2016-09-05 16:23:34

标签: c# regex winforms replace richtextbox

如何完成在标签中提取文本并转换它们的任务?

示例:

out formatted

输入:

[txtItalic]This is italic[/txtItalic] [txtBold] Bold Text [/txtBold]

输出: 这是斜体 粗体文字

我正在使用此代码提取标记之间的文本,但问题是它只需要第一个标记的文本

string ExtractString(string s, string tag)
{
    var startTag = "[" + tag + "]";
    int startIndex = s.IndexOf(startTag) + startTag.Length;
    int endIndex = s.IndexOf("[/" + tag + "]", startIndex);
    return s.Substring(startIndex, endIndex - startIndex);

}

我想要完成什么以及stackoverflow编辑器中发生了什么......

         richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Bold);

        richTextBox1.AppendText("Bold Text");

        richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Regular);

        richTextBox1.AppendText("Normal Text");

要加粗文字使用**** 和斜体**

2 个答案:

答案 0 :(得分:0)

这应该为你做的工作:

string s = "[txtItalic]This is italic[/txtItalic] [txtBold] Bold Text [/txtBold]";
//creating a list of tags
List<string> tags = new List<string>();
tags.Add("txtItalic");
tags.Add("txtBold");
//iterating through each of the tags
foreach (string tag in tags)
{
    var startTag = "[" + tag + "]";
    int startIndex = s.IndexOf(startTag) + startTag.Length;
    int endIndex = s.IndexOf("[/" + tag + "]", startIndex);
    string s1 = s.Substring(startIndex, endIndex - startIndex);
    Console.Write(s1);
}

输出:

  

这是斜体粗体文字

注意: 这只会在代码之间提取文字。

答案 1 :(得分:0)

这是一种做我认为你需要的方式:

my_df = dbGetQuery(con, my_query)

如果你使用标准的RichTextBox,那就是结果:

End result in RichTextBox control

当然这只是一个起点。如果要组合格式或任何其他功能,则必须添加该功能。 ;)