Get text from Rich Text

Anyone know how to strip the tags from a rich text object to get the actual text?
Rich Text: Redirecting to latest version of com.unity.ugui

Use case: Rich text in a drop down list, need to compare the actual text against a db field to select.
It seems unity adds the tags once it’s rich text so I need the original text string without the markup tags.

ex: availableProducts.addOption (new GUIContent (“<color=#add8e6ff>” + content [0].Name + “”));

I tried making it a string but it’s the same, even shows up in debug with the color from the color tags so the markup is passed over when assigning it to a string.

Anyone?

ty!

No one?

You proly need to remove the tags from the string yourself using string.Remove() and Replace().

Although not perfect, you can use a regular expression.

string nonRichTextString = Regex.Replace(richTextString, "<.*?>", string.Empty);
5 Likes

Try this to get pure text from rich text.
There may be errors because I just wrote,
Please let me know to fix it.

    public static T[] Arr<T>(this T tt) => new T[] { tt };
    static public readonly string[]
        DELIM_RICH_open = "</".Arr(),
        DELIM_RICH_close = ">".Arr();
    public static string SweepRich(this string msg0)
    {
        if (string.IsNullOrEmpty(msg0))
            return msg0;

        string[] msgs = msg0.Split(DELIM_RICH_open, StringSplitOptions.None);
        if (msgs.Length == 1)
            return msg0;

        for (int idx = 1; idx < msgs.Length; idx++) {
            void revert() => msgs[idx] = DELIM_RICH_open[0] + msgs[idx];
            bool invalid(string[] arr)
            {
                if (arr.Length == 1) {
                    revert();
                    return true;
                }
                return false;
            }

            string[] right = msgs[idx].Split(DELIM_RICH_close, 2, StringSplitOptions.None);
            if (invalid(right))
                continue;

            string tag = right[0];
            string[] left = msgs[idx - 1].Split(('<' + tag).Arr(), StringSplitOptions.None);
            if (invalid(left))
                continue;

            string[] leftRight = left[1].Split(DELIM_RICH_close, 2, StringSplitOptions.None);
            if (invalid(leftRight))
                continue;

            msgs[idx - 1] = left[0] + leftRight[1];
            msgs[idx] = right[1];
        }

        return String.Join(null, msgs);
    }