如何截取字符串,只显示前面几个,多截少不补 如何截取字符串,只显示前面几个,多截少不补要考虑到二个英文算一个汉字 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看你是想按字符截取还是按字节截取,按字符取直接SubString就行了,按字节的话,就先将字符串转成字节数组,截取前面的若干个,然后再转成字符串,这时要考虑半个汉字的问题。 public static string StringCut( string strInput , string strEnd , int intLen ) { strInput = strInput.Trim() ; int byteLen = Encoding.Default.GetByteCount( strInput ) ; if ( byteLen > intLen ) { string resultStr = "" ; for ( int i = 0 ; i < strInput.Length ; i++ ) { if ( Encoding.Default.GetByteCount( resultStr ) < intLen - strEnd.Length ) { resultStr += strInput.Substring( i , 1 ) ; } else { break ; } } return resultStr + strEnd ; } else { return strInput ; } } 你直接substring就行了, 应为.Net中字符是会自动判断宽字还是普通的字的如果你是要根据显示的长度来截字符串, 那就要用System.Text.Encoding.GetEncoding("gb2312").GetByteCount来数字节数 string str ="a你?bc我123"; byte[] strb = Encoding.Default.GetBytes(str); int cnt =4; byte[] subb = new byte[cnt]; Buffer.BlockCopy(strb, 0, subb, 0, cnt); string sub = Encoding.Default.GetString(subb); if ((subb[cnt-1]!='?') && (sub[sub.Length - 1] == '?')) sub = sub.Substring(0, sub.Length - 1); Console.WriteLine(sub); 求换为任意字符匹配,不是从0开始匹配字符,因为对JAVA语言不熟,找也麻烦。 关于在 web 页上显示图片(新手提问) 如何在ListBox控件将引发SelectedIndexChanged事件时让页面不刷新? 。。急。。。这个 sql 语句是什么意思? 如何在服务器端给DIV或Table赋值(高度,位置,偏移量等)? JS跳转问题 强烈要求重新选举Asp.net版主 没分了,但还是想问问。。。 我安装了快速入门示例,但都是英文的,请问如何安装中文的? 如何得到某一字符串的宽和高? 怎样等到硬盘序列号 vs2005中的web程序不生成.dll么?
{
strInput = strInput.Trim() ;
int byteLen = Encoding.Default.GetByteCount( strInput ) ;
if ( byteLen > intLen )
{
string resultStr = "" ;
for ( int i = 0 ; i < strInput.Length ; i++ )
{
if ( Encoding.Default.GetByteCount( resultStr ) < intLen - strEnd.Length )
{
resultStr += strInput.Substring( i , 1 ) ;
}
else
{
break ;
}
}
return resultStr + strEnd ;
}
else
{
return strInput ;
}
}
如果你是要根据显示的长度来截字符串, 那就要用System.Text.Encoding.GetEncoding("gb2312").GetByteCount来数字节数
byte[] strb = Encoding.Default.GetBytes(str);
int cnt =4;
byte[] subb = new byte[cnt];
Buffer.BlockCopy(strb, 0, subb, 0, cnt);
string sub = Encoding.Default.GetString(subb);
if ((subb[cnt-1]!='?') && (sub[sub.Length - 1] == '?'))
sub = sub.Substring(0, sub.Length - 1);
Console.WriteLine(sub);