How can I convert text in between <i> and </i> to italics and make the tags disappear in richtextbox C#

StackOverflow https://stackoverflow.com/questions/21680306

문제

I have this text apearing in a richtextbox: The path of those on whom Thou hast bestowed < i >Thy< /i > blessings, those who have not incurred < i >Thy< /i > displeasure, and those who have not gone astray. Whatever is between and should turn into italics and the tags shouldn't appear.

I used this code:

richTextBox1.Select(richTextBox1.Text.IndexOf("</i>") + 1, richTextBox1.Text.IndexOf("<i>") - richTextBox1.Text.IndexOf("</i>") - 1);
Font italicFont = new Font("Cambira", 14, FontStyle.Italic);
richTextBox1.SelectionFont = italicFont;

But this code isn't working, how do I fix that?

올바른 솔루션이 없습니다

다른 팁

You'd be better of using Regular Expressions to replace the text before you put it in the RichTextBox. However, to do it with code, you could use this...

    int token = -1;
    int token2 = -1;

    richTextBox1.Text = richTextBox1.Text.Replace(" <", "<");
    richTextBox1.Text = richTextBox1.Text.Replace("< ", "<");
    richTextBox1.Text = richTextBox1.Text.Replace(" >", ">");
    richTextBox1.Text = richTextBox1.Text.Replace("> ", ">");

    while (richTextBox1.Text.IndexOf("<i>") > -1)
    {
      token = richTextBox1.Text.IndexOf("<i>");
      token2 = richTextBox1.Text.IndexOf("</i>", token) + 4;
      string clip = richTextBox1.Text.Substring(token, token2 - token);

      Font italicFont = new Font("Cambira", 14, FontStyle.Italic);
      richTextBox1.Select(token, token2 - token);
      richTextBox1.SelectionFont = italicFont;
      richTextBox1.SelectedText = " " + clip.Replace("<i>", "").Replace("</i>", "") + " ";
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top