●字●符●串●等●宽●分●割●,没有人会吗?失望
字符串里含有符号,字母汉字,标点......
论坛的文章列表, 文章标题列,如果文章标题超过20个汉字的宽度,只显示20个汉字的宽度。(一般两个字母或者数字占一个汉字的宽度)。
不要教我用Ascii码的方式,因为我已经试过了不好用!
[解决办法]
可以试试用 Graphics.MeasureString ,
这个方法可以得到显示字符串需要区域的大小,你用它得到宽度,太宽就一个一个去掉最后字符
[解决办法]
string test1= "abcdef123456789 ";
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);
[解决办法]
简单问题复杂化,样式表轻松搞定,L@_@K
<html>
<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;
}
[解决办法]
外边进行封装,写为:
<table border= "0 " cellpadding= "0 " cellspacing= "0 ">
<tr>
<td nowrap>
<span style= "text-overflow: ellipsis; overflow-x: hidden; width: 300px; "> 水果拼盘:西瓜、苹果、哈密瓜,或者任何别的什么 </span>
</td>
</tr>
</table>
[解决办法]
另外,例如:
<table border= "0 " cellpadding= "0 " cellspacing= "0 ">
<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;
}