String a bold / Italic

0

I am trying to split a query string by intros, and after that check if there are Bold or Italica tags, if there are any that string will become Bold / Italic depending on the situation. I do it but it does not work, can someone tell me why?

 TableCell tc8 = new TableCell();

                Table t8 = new Table();
                string text8 = dr["OBSFX"].ToString();
                char d = '\n';
               string[] substr8 = text8.Split(d);
                foreach (string s in substr8)
                {
//La string s puede tener o no una parte escrita en bold (<b>asdf</b> ghjk ) por ejemplo
//La parte bold es la que tiene que estar en negrita obviamente
                    TableRow trS = new TableRow();
                    TableCell tcS = new TableCell();

                    tcS.Text = s;
                    if (s.Contains("<b>")&& s.Contains("</b>"))
                    {
                        tcS.Font.Bold=true;
                    }
                    if (s.Contains("<i>")&& s.Contains("</i>"))
                    {
                        tcS.Font.Italic = true;
                    }
                    trS.Cells.Add(tcS);
                    t8.Rows.Add(trS);
                }
                tc8.Controls.Add(t8);

I've also tried with

  Table t8 = new Table();
                string text8 = dr["OBSFX"].ToString();
                char d = '\n';
               string[] substr8 = text8.Split(d);
                //separar por "enter"
                foreach (string s in substr8)
                {
                    TableRow trS = new TableRow();
                    TableCell tcS = new TableCell();
                     Label l = new Label();

                    if (s.Contains("<b>") && s.Contains("</b>"))
                    {
                        int n = s.IndexOf("<b>");
                        int nc = s.IndexOf("</b>");
                        l.Text = s.Substring(0, n);
                        l.Text += "<b>" + s.Substring(n + 3, nc - 3) + "</b>";
                        l.Text = s.Substring(nc + 4);
                        tcS.Controls.Add(l);

                    }
                    else {
                        l.Text = s;
                        tcS.Controls.Add(l);

                    }

But even so, it still does not enter the verification, it is as if it did not detect the Bold labels

Also with the regular expressions that I put @DanMiranda and nothing :(  Another of the checks I have made is the following:

 foreach (string s in substr8)
                {
                    TableRow trS = new TableRow();
                    TableCell tcS = new TableCell();
                     Label l = new Label();
                    if ((s.IndexOf("<b>")!=-1 )|| (s.IndexOf("</b>")!=-1))
                    {
                        int n = s.IndexOf("<b>");
                        int nc = s.IndexOf("</b>");
                        l.Text = s.Substring(0, n);
                        l.Text += "<b>" + s.Substring(n + 3, nc - 3) + "</b>";
                        l.Text = s.Substring(nc + 4);
                        tcS.Controls.Add(l);

                    }
                    else if ((s.IndexOf("<i>") != -1) || (s.IndexOf("</i>") != -1))
                    {
                        int n = s.IndexOf("<i>");
                        int nc = s.IndexOf("</i>");
                        l.Text = s.Substring(0, n);
                        l.Text += "<i>" + s.Substring(n + 3, nc - 3) + "</i>";
                        l.Text = s.Substring(nc + 4);
                        tcS.Controls.Add(l);
                    }
                    else
                    {
                        l.Text = s;
                        tcS.Controls.Add(l);

                    }

My despair gradually increases: |

    
asked by Geraniego 24.03.2017 в 16:04
source

1 answer

0

Make comparisons with regular expressions, they are more accurate and easier to maintain.

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        // Busca etiquetas b en mayusculas y minusculas.
        string boldRegex = @"<(B|b)>.*<\/(B|b)>";

        // Busca etiquetas i en mayusculas y minusculas.
        string italicRegex = @"<(I|i)>.*<\/(I|i)>";

        string text8 = "<i><B>unTextodePrueba</b></i>";

        if (Regex.Matches(text8, boldRegex).Count > 0){
            // Is Bold
            Console.WriteLine("Is Bold");
        }

        if (Regex.Matches(text8, italicRegex).Count > 0){
            // Is Italic
            Console.WriteLine("Is Italic");
        }

    }
}
    
answered by 24.03.2017 в 17:43