利用正则表达式计算含有中文的字符串长度

简介:
None.gif using System;
None.gif using System.Text.RegularExpressions;
None.gif
None.gif namespace LangZi
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
InBlock.gif    
/// StringHelper 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class StringHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        public StringHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            //
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        GetLength#region GetLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        
/// 返回包含中文字符的字符串长度
InBlock.gif        
/// C# 的string.Length中中文字只做1位统计,所以要将其转换为2位
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strSource">要统计长度的字符串变量</param>
ExpandedSubBlockEnd.gif        
/// <returns>字符串长度</returns>

InBlock.gif        public static int GetLength(string strSource)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
InBlock.gif            int nLength = strSource.Length;
InBlock.gif
InBlock.gif            for(int i=0; i<strSource.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
InBlock.gif                if (regex.IsMatch(strSource.Substring(i,1))) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
InBlock.gif                    nLength++;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            return nLength;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        #endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

使用:

None.gif int iLength= LangZi.StringHelper.GetLength(source)


土人用最土的办法,以求实现自己的目标,看了 银河兄的 C#中的字符编码问题 一文,发现有更好更完善的方法:

None.gif using System;
None.gif using System.Text;
None.gif
None.gif namespace LangZi
ExpandedBlockStart.gif ContractedBlock.gif dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
InBlock.gif    
/// StringHelper 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class StringHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
InBlock.gif        public StringHelper()
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif            //
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        GetLength#region GetLength
ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
InBlock.gif        
/// 返回包含中文字符的字符串长度
InBlock.gif        
/// C# 的string.Length中中文字只做1位统计,所以要将其转换为2位
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="strSource">要统计长度的字符串变量</param>
ExpandedSubBlockEnd.gif        
/// <returns>字符串长度</returns>

InBlock.gif        public static int GetLength(string strSource)
ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
InBlock.gif
             return Encoding.GetEncoding("GB18030").GetBytes(strSource).Length; 
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        #endregion

ExpandedSubBlockEnd.gif    }

本文转自浪子博客园博客,原文链接:http://www.cnblogs.com/walkingboy/archive/2005/08/24/222005.html,如需转载请自行联系原作者
目录
相关文章
|
1月前
|
JavaScript 前端开发
JavaScript随手笔记 --- 用正则表达式匹配字符串是否为运算公式
JavaScript随手笔记 --- 用正则表达式匹配字符串是否为运算公式
|
5月前
|
Python
133 python高级 - 正则表达式(原始字符串)
133 python高级 - 正则表达式(原始字符串)
29 0
|
6月前
|
C++
使用 ABAP 正则表达式提高字符串解析的执行效率
使用 ABAP 正则表达式提高字符串解析的执行效率
60 0
|
7月前
|
程序员
用正则表达式判断字符串形式正误(例:判断电话号码属地是否为中国大陆)
用正则表达式判断字符串形式正误(例:判断电话号码属地是否为中国大陆)
44 0
|
1月前
|
Linux Perl
使用awk和正则表达式过滤文本或字符串 - 详细指南和示例
使用awk和正则表达式过滤文本或字符串 - 详细指南和示例
64 0
|
8月前
|
Java
避免分割字符串的常见错误:正则表达式元字符未转义
在编程和文本处理中,我们经常需要将一个字符串按照特定的分隔符拆分成多个部分。为了实现这一目标,我们使用分割函数或正则表达式来定义我们所需的分隔符。 在 Java 的 String 类的 split() 函数中,如果你使用字符串参数作为分隔符,例如 split(","),它会按照每个字符作为分隔符进行字符串拆分。
58 0
|
4月前
|
算法
【面试算法——动态规划 21】正则表达式匹配(hard)&& 交错字符串
【面试算法——动态规划 21】正则表达式匹配(hard)&& 交错字符串
|
9月前
|
JavaScript 前端开发 索引
javascript截取两个符号之间的字符串(2):lastIndexOf匹配和正则表达式匹配
javascript截取两个符号之间的字符串(2):lastIndexOf匹配和正则表达式匹配
273 0
|
4月前
通过正则表达式获取字符串中的省市区
通过正则表达式获取字符串中的省市区
52 0
通过正则表达式获取字符串中的省市区
|
4月前
|
Java
每日一刷《剑指offer》字符串篇之正则表达式匹配
每日一刷《剑指offer》字符串篇之正则表达式匹配
51 0
每日一刷《剑指offer》字符串篇之正则表达式匹配

热门文章

最新文章