C# 截取字符串方法(包含中英混合)

时间:2023-03-08 17:03:57
 private string GetByteString(string center, int maxlen, string endStr)
{
string temp = center.Substring(, (center.Length < maxlen + ) ? center.Length : maxlen + );
byte[] encodedBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(temp);
string outputStr = "";
int count = ;
for (int i = ; i < temp.Length; i++)
{
if ((int)encodedBytes[i] == )
count += ;
else
count += ; if (count <= maxlen - endStr.Length)
outputStr += temp.Substring(i, );
else if (count > maxlen)
break;
}
if (count <= maxlen)
{
outputStr = temp;
endStr = "";
}
outputStr += endStr;
return outputStr;
}