I’m building sitemaps and I need a way to quickly check how many UTF-8 encoded bbytes StringBuilder currently contains?
The naive way to do this would be to:
Encoding.UTF8.GetBytes(builder.ToString()).Length
But isn’t this a bit bloated?
Using builder.Length doesn’t work as certain charactes resolved to 2 bytes such as ÅÄÖ.
You could use this:
Unfortunately, unlike Java where there is a CharSequence interface, you cannot directly process the StringBuilder without first converting it to a string.