怎么让汉字的字节和数字,字母的字节一样
如何让汉字的字节和数字,字母的字节一样?
如题:
我在程序中判断一个标题的长度如果大于5,就进行截取
string title="123呵呵";
if(title.Lenth > 5)
{
title.Substring(0,5)+"...";
}
这是按长度截取的,如何按字节截取呢?
title.getBytes().Lenth这个方法在C#中好像没有唉,,,,
------解决方案--------------------
如题:
我在程序中判断一个标题的长度如果大于5,就进行截取
string title="123呵呵";
if(title.Lenth > 5)
{
title.Substring(0,5)+"...";
}
这是按长度截取的,如何按字节截取呢?
title.getBytes().Lenth这个方法在C#中好像没有唉,,,,
------解决方案--------------------
- C# code
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);
------解决方案--------------------
你想 一个汉字2字节 你截取一个字节 那截取出来的 是什么子
就拿“一”这字来说 你把截取其中一个字节出来 那是什么字?