码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-07-23 13:39:16      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

 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;

    }

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

标签:

原文地址:http://www.cnblogs.com/DTWolf/p/4670149.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!