zoukankan      html  css  js  c++  java
  • 中英文混合时截取固定字符串长度(中文算两个长度)

    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;
        }
  • 相关阅读:
    Django搭建环境
    python切片
    python数据类型
    jquery 淡入淡出属性
    Jquery Tab切换
    jQuery Clone方法
    jQuery属性操作
    python 变量以及循环
    获取网站目录
    posting-jsonobject-with-httpclient-from-web-api
  • 原文地址:https://www.cnblogs.com/xueyuan299/p/1545372.html
Copyright © 2011-2022 走看看