惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园 - 叶小钗
小众软件
小众软件
WordPress大学
WordPress大学
I
InfoQ
Last Week in AI
Last Week in AI
Vercel News
Vercel News
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
腾讯CDC
D
DataBreaches.Net
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - [曾恩]

IIS7根据PID查找对应的站点 CentOS 6.4 i386 版本安装 FastDFS、使用Nginx作为文件访问WEB服务器 Mysql导出表结构、表数据 编译器错误消息: CS0016: 未能写入输出文件“c:/Windows/Microsoft.NET/Framework/v4.0.50727/Temporary ASP.NET Files/root .... 拒绝访问。 LVS + KEEPALIVED + WINDOWS SERVER 2008 R2 ------高可用负载均衡 windows中IIS7配置PHP(FastCGI模式-PHP在IIS下运行最高效最稳定的模式) 因为数据库正在使用,所以无法获得对数据库的独占访问权 SQL 2005 / SQL 2008 bit(位)、byte(字节)、字符、英文字母、中文 HTML常用字符串替换 设置winform中webBrower控件所使用的IE版本 C#中的类修饰符 partial 在windows下安装环回适配器(Microsoft Loopback Adapter) Linux(CentOS)安装分区方案 Linux(CentOS)日常操作命令 Linux(CentOS)目录操作命令、文件操作命令、压缩解压缩命令 Linux(CentOS)挂载移动硬盘,实现文件拷贝、备份 windows server 2003 运行 asp.net MVC MySql命令行下导出、导入数据 SQL中的循环、for循环、游标
获取包含中文字符串的长度、截取包含中文的字符串
[曾恩] · 2012-04-28 · via 博客园 - [曾恩]
        /// <summary>
        /// 截取包含中文、英文、中英文混合字符的字符串
        /// </summary>
        /// <param name="s"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static String Substring(this string s, int length)
        {
            if (s.GetLength() > length)
            {
                if (s == null || s.Length == 0 || length <= 0)
                {
                    return string.Empty;
                }

                int l = s.Length;

                #region 计算长度
                int clen = 0;
                while (clen < length && clen < l)
                {
                    //每遇到一个中文,则将目标长度减一。
                    if ((int)s[clen] > 128) { length--; }
                    clen++;
                }
                #endregion

                if (clen < l)
                {
                    return s.Substring(0, clen) + "...";
                }
                else
                {
                    return s;
                }
            }
            else
            {
                return s;
            }
        }

        /// <summary>
        /// 获取中文、英文、中英文混合字符串长度
        /// </summary>
        /// <param name="strSource"></param>
        /// <returns></returns>
        public static int GetLength(this string strSource)
        {
            return Encoding.GetEncoding("GB18030").GetBytes(strSource).Length;
        }