获得字符串的中文长度

2014-02-15 23:15:16  访问(4553) 赞(0) 踩(0)

/// 
/// 获得字符串长度[中文长度]
/// 
/// 
/// 
public static int GetLength(string str)
{
    if (str == null || str.Length == 0)
        return 0;

    ASCIIEncoding ascii = new ASCIIEncoding();

    int tempLen = 0;

    byte[] s = ascii.GetBytes(str);

    int iLen = s.Length;

    for (int i = 0; i < iLen; i++)
    {
        if ((int)s[i] == 63)
        {
            tempLen += 2;
        }
        else
        {
            tempLen += 1;
        }
    }

    return tempLen;
}


标签:获得字符串的中文长度    C#代码    字符串常用操作 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)