如何完成在标签中提取文本并转换它们的任务?
示例:
输入:
[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");
要加粗文字使用**** 和斜体**
答案 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)