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

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - guanfei

如何用Nero刻录ape(带cue)cd镜像?详细点,下载啥,装到哪里 Managing assembly version numbers using Visual Studio .NET and Visual SourceSafe RMA是什么 Page.RegisterClientScriptBlock和Page.RegisterStartupScript有何区别 - guanfei - 博客园 button style - guanfei - 博客园 SQL-SERVER取得中文拼音的函数 DesignMode :: Windows Forms designer and DesignMode property issues 项目管理相关 什么是毅力? The 30 Minute Regex Tutorial Exception 的性能的影响不大。 调试WebService的一个很好的工具 漫谈C#编程中的多态与new关键字 Object synchronization method was called from an unsynchronized block of code. 关于C#中timer类 BUG: The Elapsed event of the System.Timers.Timer class is not raised in a Windows service 全文搜索参考 sql 2000 2005 创建全文索引, 中文的需要中文版的sql sql server 2000 全文索引全解(配合ASP.NET)
C#区分中英文统计字符串的长度
guanfei · 2007-08-19 · via 博客园 - guanfei

转 http://blog.5iaspx.com/article.asp?id=312

我们都知道C#中的string自已有一个Length属性,用来统计字符串的长度,如果字符都是中文或都是英文,那没什么问题,但如果出现中英文混合的 情况,我们的统计结果就不太准确了,因为在计算机中,中文占两个字节的空间,英文占一个字节的空间,而string的Length不具备区分中英文的功 能,所以它计算出来的长度并不准确,如我们在网页上显示字符时,有时候不能超过一定的长度,如果字符串是中英文混合,那载取的字符串长短不一,严重影响美 观。

    当然,我们可以用一种非常简单的方法来计算字符串的长度,而且是中文算两个字符,英文算一个字符,下面给出我一直在用的一个方法的源代码,供大家参考:

      /// <summary>  
      /// 计算文本长度,区分中英文字符,中文算两个长度,英文算一个长度
      /// </summary>
      /// <param name="Text">需计算长度的字符串</param>
      /// <returns>int</returns>
      public int Text_Length(string Text)
      {
            int len=0;

            for(int i=0;i<Text.Length;i++)
            {
                byte[] byte_len = Encoding.Default.GetBytes(Text.Substring(i,1));
                if(byte_len.Length>1)
                    len += 2; 

//如果长度大于1,是中文,占两个字节,+2
                else
                    len += 1;  //如果长度等于1,是英文,占一个字节,+1
            }

            return len;
        }

    当然别忘了加上'using System.Text;',对System.Text的引用哦!!!