• C#截取中英文混合字符串分行显示


     private int GetStrByteLength(string str)

        {

            return System.Text.Encoding.Default.GetByteCount(str);

        }

        private string SubStrLenth(string str, int startIndex, int length)

        {

            int strlen = GetStrByteLength(str);

            if (startIndex + 1 > strlen)

            {

                return "";

            }

            int j = 0;//记录遍历的字节数

            int L = 0;//记录每次截取开始,遍历到开始的字节位,才开始记字节数

            int strW = 0;//字符宽度

            bool b = false;//当每次截取时,遍历到开始截取的位置才为true

            string restr = string.Empty;

            for (int i = 0; i < str.Length; i++)

            {

                char C = str[i];

                if (ISChinese(C))

                {

                    strW = 2;

                }

                else

                {

                    strW = 1;

                }

                if ((L == length-1) && (L + strW > length))

                {

                    b = false;

                    break;

                }

                if (j >= startIndex)

                {

                    restr += C;

                    b = true;

                }

              

                j += strW;



                if (b)

                {

                    L += strW;

                    if (((L + 1) > length))

                    {

                        b = false;

                        break;

                    }

                }



            }

            return restr;

        }

  • 相关阅读:
    网络CCNA基础了解
    KVM 安装 VMware 虚拟机
    [转载]JS浏览器兼容性问题
    java中数组是不是对象?
    [转载]request.getServletPath()方法
    weblogic下更改jsp不生效的解决办法
    java之args[0]
    docker小demo
    eclipse优化
    [转载]oracle建表语句大全
  • 原文地址:https://www.cnblogs.com/DTWolf/p/4670149.html
Copyright © 2020-2023  润新知