程序园

弹奏键盘人生,拂去青春的尘土,留下的只有岁月的痕迹
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
private string subMixString(string str, int subBytes)
    {
        int bytes = 0;   //   用来存储字符串的总字节数
        for (int i = 0; i < str.Length; i++)
        {
            if (bytes >= subBytes)
            {
                return str.Substring(0, i) + "……";
            }
            if (str[i] < 256)
            {
                bytes += 1;   //   英文字符的字节数看作1
            }
            else
            {
                bytes += 2;   //   中文字符的字节数看作2
            }
        }
        return str;
    }