字符串里含有符号,字母汉字,标点...... 
论坛的文章列表, 文章标题列,如果文章标题超过20个汉字的宽度,只显示20个汉字的宽度。(一般两个字母或者数字占一个汉字的宽度)。
不要教我用Ascii码的方式,因为我已经试过了不好用!

解决方案 »

  1.   

    可以试试用 Graphics.MeasureString ,
    这个方法可以得到显示字符串需要区域的大小,你用它得到宽度,太宽就一个一个去掉最后字符
      

  2.   

    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);
      

  3.   

    简单问题复杂化,样式表轻松搞定,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>
      

  4.   

    很多方法可以实现
    下面是一个用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; 
    }
      

  5.   

    外边进行封装,写为:<table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td nowrap>
                <span style="text-overflow: ellipsis; overflow-x: hidden; width: 300px;">水果拼盘:西瓜、苹果、哈密瓜,或者任何别的什么</span>
            </td>
        </tr>
    </table>
      

  6.   

    一般两个字母或者数字占一个汉字的宽度
    ——————————————————————————————————————————
    这有点中了流感病毒了。csdn比较怪,你注意看这个页面,它选择了比较难看的字体,英文的间隔有的很宽有的很密。尽管这么难看,也并不敢保证每一个英文、数字、标点的宽度完全一致。汉字怎么可能保证在丰富的字体(例如有的是隶书、有的是宋体)下每一个等于两个英文宽?
      

  7.   

    另外,例如:<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>
    又如何应付呢?
      

  8.   

    我理解搂主要截取文字?
    那看看这个。方法很土,不过挺好用。
            /// <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;
            }
      

  9.   

    很多方法的,ASCII不行,和VC++不一样了,但Unicode可以,从网上容易搜到汉字的编码区间,看是否在这个区间就可以了。
      

  10.   

    public string CutString(string strString, int intMaxLength)
        {
    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() + "...";
        }
      

  11.   


     yixianggao() 正解
      

  12.   

    C#汉字是一个字符,就按照一般char处理就可以了