如题:
我在程序中判断一个标题的长度如果大于5,就进行截取
string title="123呵呵";
if(title.Lenth > 5)
{
   title.Substring(0,5)+"...";
}
这是按长度截取的,如何按字节截取呢?
title.getBytes().Lenth这个方法在C#中好像没有唉,,,,

解决方案 »

  1.   

    System.Text.Encoding.Default.GetByteCount("youstring");
      

  2.   

    public static string getStr(string s,int l) 
    {     
    string temp = s ;  if (Regex.Replace(temp,"[^\x00-\xff]","zz",RegexOptions.IgnoreCase).Length<=l) 

    return temp; 

    for (int i=temp.Length;i>=0;i--) 

    temp = temp.Substring(0,i); 
    if (Regex.Replace(temp,"[^\x00-\xff]","zz",RegexOptions.IgnoreCase).Length<=l) 

    return temp + ""; 
    }     

    return ""; 


    函数中参数s是字符串,l是字节长度,比如:
    string content = "中国人中国人"; 
    content = getStr(content,3); 
      

  3.   

    或者把里面的正则换成汉字[\u4e00-\u9fa5]
    我上面的是双字节的