美文网首页
C#区分中英文按照指定长度截取字符串的代码

C#区分中英文按照指定长度截取字符串的代码

作者: 不是砖家 | 来源:发表于2019-01-05 12:08 被阅读0次

将内容过程中比较常用的内容备份一下,下面的内容是关于C#区分中英文按照指定长度截取字符串的内容,应该能对小伙伴有所好处。

        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;

        }

相关文章

网友评论

      本文标题:C#区分中英文按照指定长度截取字符串的代码

      本文链接:https://www.haomeiwen.com/subject/pfyrrqtx.html