I need length of a rich text string in terms of characters. How can I do it without manually stripping it of all tags?
You can create a simple helper method like this:
public int TextLength(string richText)
{
int len = 0;
foreach (var ch in richText)
{
bool inTag = false;
if (ch == '<'')
{
inTag = true;
}
else if (ch == '>')
{
inTag = false;
}
if (inTag)
{
continue;
}
len++;
}
return len;
}
DISCLAIMER : i never tested this, but you can get the rough idea.
1 Like
Yeah, that would work. Thx!
…and before anyone else just grabs the above optimistically, here’s it working:
public int TextLength(string richText)
{
int len = 0;
bool inTag = false;
foreach (var ch in richText)
{
if (ch == '<')
{
inTag = true;
continue;
}
else if (ch == '>')
{
inTag = false;
}
else if (!inTag)
{
len++;
}
}
return len;
}