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("​");
}
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 ß? What can I do to calculate the actual length of the string (which appears in the browser)? A string like ♡♥♡ ♥ contains only 2 characters (hearts) in the browser, but its length is 14.
source
share