C#截取指定长度的方法

来源:互联网 发布:专题片男生配音软件 编辑:程序博客网 时间:2024/03/29 22:05
 public static string GetSubString(string str, int length)
    {
        string temp = str;
        int j = 0;
        int k = 0;
        for (int i = 0; i < temp.Length; i++)
        {
            if (Regex.IsMatch(temp.Substring(i, 1), @"[\u4e00-\u9fa5]+"))
           {
                j += 2;
            }
            else
            {
                j += 1;
            }
            if (j <= length)
            {
                k += 1;
            }
            if (j >= length)
            {
                return temp.Substring(0, k);
            }
        }
        return temp;
    }