日期:2014-05-19  浏览次数:20418 次

●字●符●串●等●宽●分●割●,没有人会吗?失望
字符串里含有符号,字母汉字,标点......  
论坛的文章列表,   文章标题列,如果文章标题超过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>