字符串里含有符号,字母汉字,标点......
论坛的文章列表, 文章标题列,如果文章标题超过20个汉字的宽度,只显示20个汉字的宽度。(一般两个字母或者数字占一个汉字的宽度)。
不要教我用Ascii码的方式,因为我已经试过了不好用!
论坛的文章列表, 文章标题列,如果文章标题超过20个汉字的宽度,只显示20个汉字的宽度。(一般两个字母或者数字占一个汉字的宽度)。
不要教我用Ascii码的方式,因为我已经试过了不好用!
这个方法可以得到显示字符串需要区域的大小,你用它得到宽度,太宽就一个一个去掉最后字符
string test2="abcdef中国人123";//string的Length属性得到的是字符个数
Console.WriteLine(test1.Length);
Console.WriteLine(test2.Length);
//但有时候是要字节数
byte[] b=System.Text.Encoding.Default.GetBytes(test2);
Console.WriteLine(b.Length);
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<style type="text/css">
.clsTitle
{
width: 150px;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head> <body>
<table border="1">
<tr>
<td nowrap><div class="clsTitle"><a href="" target="_blank">水果拼盘:西瓜、苹果、哈密瓜</a></div></td>
<td>2007-4-17</td>
</tr>
<tr>
<td nowrap><div class="clsTitle">水果拼盘:西瓜、苹果、哈密瓜</div></td>
<td>2007-4-17</td>
</tr>
</table>
</body>
</html>
下面是一个用javascript实现的方法,改改就成
function getInterceptedStr1(sSource,iLen)
{
var d=new Date();
var el="",hzlen;
var re=new RegExp("^(.{"+(iLen)+"}).*$","i")
try{hzlen=sSource.replace(re,"$1").match(/[^\x00-\xff]/g).length}catch(e){hzlen=0;}
if(sSource.length+hzlen<=iLen)return sSource;
re.compile("^(.{"+(iLen-Math.ceil(hzlen/2))+"}).*$","i")
alert(new Date()-d+"ms")
return sSource.replace(re,"$1").slice(0,iLen-3)+el;
}
<tr>
<td nowrap>
<span style="text-overflow: ellipsis; overflow-x: hidden; width: 300px;">水果拼盘:西瓜、苹果、哈密瓜,或者任何别的什么</span>
</td>
</tr>
</table>
——————————————————————————————————————————
这有点中了流感病毒了。csdn比较怪,你注意看这个页面,它选择了比较难看的字体,英文的间隔有的很宽有的很密。尽管这么难看,也并不敢保证每一个英文、数字、标点的宽度完全一致。汉字怎么可能保证在丰富的字体(例如有的是隶书、有的是宋体)下每一个等于两个英文宽?
<tr>
<td nowrap>
<span style="text-overflow: ellipsis; overflow-x: hidden; width: 300px;">
水果拼盘:西瓜、苹果、哈密瓜,<span style="font-size:large">或者任何别的</span>什么</span>
</td>
</tr>
</table>
又如何应付呢?
那看看这个。方法很土,不过挺好用。
/// <summary>
/// Cuts string by the given byte count.
/// </summary>
/// <param name="text">The string to be cut.</param>
/// <param name="bytes">The count of bytes to be cut.</param>
/// <returns>
/// The cut string.
/// Once the number of <paramref name="bytes"/> is grater than the
/// length of <paramref name="text"/>, return the whole string.
/// </returns>
private string CutStringByByte(string text, int bytes)
{
// if less tan wanted, return the whole string.
int nCount = Encoding.Default.GetByteCount(text); if (nCount <= bytes)
{
return text;
} // Does cut
string strRet = string.Empty; // the text to be returned.
int nCut = 0; // the bytes of cut string.
int nCutIndex = 0; // the index of the character in text. nCut += Encoding.Default.GetByteCount(text[nCutIndex].ToString()); while (nCut <= bytes)
{
strRet += text[nCutIndex].ToString();
nCutIndex++; nCut += Encoding.Default.GetByteCount(text[nCutIndex].ToString());
} return strRet;
}
{
if (Encoding.GetEncoding(ENCODING).GetByteCount(strString) < intMaxLength)
return strString; StringBuilder strBuilder = new StringBuilder(); int nLength = 0; for (int i = 0; i < strString.Length; i++)
{
nLength += Encoding.GetEncoding(ENCODING).GetByteCount(strString[i].ToString()); if (nLength > intMaxLength)
continue; strBuilder.Append(strString[i]);
} return strBuilder.ToString() + "...";
}
yixianggao() 正解