mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-05-14 18:12:43 -04:00
Original TruncateBytes extension method wasn't working right, seemed to include an extra character which would put us over the limit. Replaced it and new method seems to be working as expected albeit not as nice looking.
This commit is contained in:
@@ -115,10 +115,18 @@ public static class Extensions
|
||||
|
||||
public static string TruncateBytes(this string s, int limitBytes)
|
||||
{
|
||||
return Encoding.UTF8.GetString(
|
||||
Encoding.UTF8.GetBytes(s)
|
||||
.Take(limitBytes)
|
||||
.ToArray()
|
||||
).TrimEnd();
|
||||
if (string.IsNullOrEmpty(s) || limitBytes <= 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
if (s.Utf8LengthBytes() <= limitBytes)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
var bytes = Encoding.UTF8.GetBytes(s);
|
||||
var charCount = Encoding.UTF8.GetCharCount(bytes, 0, limitBytes);
|
||||
return s.Substring(0, charCount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user