How to enable word interruption function by length without separation inside html-encoded special characters

I would like to implement a functionality that inserts a text TAG if the word is too long to appear on one line.

    protected string InstertWBRTags(string text, int interval)
{
    if (String.IsNullOrEmpty(text) || interval < 1 || text.Length < interval)
    {
        return text;
    }
    int pS = 0, pE = 0, tLength = text.Length;
    StringBuilder sb = new StringBuilder(tLength * 2);

    while (pS < tLength)
    {
        pE = pS + interval;
        if (pE > tLength)
            sb.Append(text.Substring(pS));
        else
        {
            sb.Append(text.Substring(pS, pE - pS));
            sb.Append("&#8203;");//<wbr> not supported by IE 8
        }
        pS = pE;
    }
    return sb.ToString();
}

Problem: what should I do if the text contains special html-encoded characters? What can I do to prevent the insertion of a TAG inside &szlig;? What can I do to calculate the actual length of the string (which appears in the browser)? A string like &#9825;&#9829;♡ ♥ contains only 2 characters (hearts) in the browser, but its length is 14.

+3
source share
2 answers

, a, , , #, , , ( ). .

Java

int count = 0;

        for(int i = 0; i < text.length(); i++) {

            if(text.charAt(i) == '&') {
                i  = text.indexOf(';', i) + 1; // what, from
            }

            count++;

        }

0

Source: https://habr.com/ru/post/1755762/


All Articles