35 lines
731 B
C#
35 lines
731 B
C#
using System.Text;
|
|
|
|
public static class ObjectExtension
|
|
{
|
|
public static string ToEllipsisString(this string message, int length)
|
|
{
|
|
var index = 0;
|
|
var sb = new StringBuilder();
|
|
foreach (var item in message)
|
|
{
|
|
if (IsChineseCharacter(item))
|
|
{
|
|
index += 2;
|
|
}
|
|
else
|
|
{
|
|
index += 1;
|
|
}
|
|
|
|
sb.Append(item);
|
|
if (length <= index)
|
|
{
|
|
return sb.Append("...").ToString();
|
|
}
|
|
}
|
|
|
|
return sb.ToString();
|
|
}
|
|
|
|
private static bool IsChineseCharacter(char c)
|
|
{
|
|
return (19968 <= (int)c) && ((int)c <= 171941);
|
|
}
|
|
}
|