能实现
“现有新闻数据表news,其中有新闻标题title字段。
请问,我如何能将title字段中取出的数据,截取它的指定长度汉字,超出部分以"..."代替”
这也可以

解决方案 »

  1.   

    给你个GRIDVIEW的例子.楼主再改改,.
    /// <summary>
            /// gridView字符过长截断显示
            /// </summary>
            /// <param name="GV">gridView</param>
            /// <param name="num">字符显示长度</param>
            public static void DataViewShow(GridView GV, int num)
            {
                for (int i = 0; i < GV.Rows.Count; i++)
                {
                    for (int j = 0; j < GV.Columns.Count; j++)
                    {
                        GV.Rows[i].Cells[j].ToolTip = GV.Rows[i].Cells[j].Text;
                        if ((GV.Rows[i].Cells[j].Text).Length > num)
                        {
                            GV.Rows[i].Cells[j].Text = (GV.Rows[i].Cells[j].Text).Substring(0, num) + "...";
                        }
                    }
                }
            }
      

  2.   

    <%# TrimString(Eval("Title"))%>TrimString写到后台TrimString (object str){
      if(str.ToString().Length>8){
      return str.ToString().Substring(0,8);
     }
    else return str.ToString()
    }
      

  3.   

    string   a="sssssssssss";   
      string   b;   
        
      if(a.length>10)   
      b=a.substring(0,10)+"...";
      

  4.   

    /// <summary>
            /// 将指定字符串按指定长度进行剪切,
            /// </summary>
            /// <param name="oldStr">需要截断的字符串</param>
            /// <param name="maxLength">字符串的最大长度</param>
            /// <param name="endWith">超过长度的后缀</param>
            /// <returns>如果超过长度,返回截断后的新字符串加上后缀,否则,返回原字符串</returns>
            public static string StringTruncat(string oldStr, int maxLength, string endWith)
            {
                if (string.IsNullOrEmpty(oldStr))
                    // throw new NullReferenceException("原字符串不能为空");
                    return oldStr + endWith;
                if (maxLength < 1)
                    throw new Exception("返回的字符串长度必须大于[0]");
                if (oldStr.Length > maxLength)
                {
                    string strTmp = oldStr.Substring(0, maxLength);
                    if (string.IsNullOrEmpty(endWith))
                        return strTmp;
                    else
                        return strTmp + endWith;
                }
                return oldStr;
            }调用:
    <%# StringTruncat(Eval("Title").ToString(),20,"...")%>
      

  5.   

    xray2005(风车车--要飞翔,必须靠自己!) <%# StringTruncat(Eval("Title").ToString(),20,"...")%>
    这是什么意思?
      

  6.   

    xray2005(风车车--要飞翔,必须靠自己!) <%# StringTruncat(Eval("Title").ToString(),20,"...")%>
    这是什么意思?
    =======================================================
    明白了^-^
      

  7.   

    如果我需要讲title这一列绑定在DataGrid中的新闻标题这列中该怎么做?
    注意,是DataGrid,不是DataView
      

  8.   

    {display: block;width: 200px;overflow: hidden;white-space: nowrap;-o-text-overflow: ellipsis;text-overflow: ellipsis;}试着加一下这个CSS
      

  9.   

    public string getsstr(string msg)
    {
      if(msg.length>7)
          return msg.substring(0,7)+"...";
      else
         return msg+"...";
    }
      

  10.   

    要是给你的CSS不行  我自己写了一个 算像素的  比较准 但是长了也有误差
      

  11.   

    dgl_1225() ( ) 信誉:100  2007-8-23 10:37:04  得分: 0  
     
     
       
    {display: block;width: 200px;overflow: hidden;white-space: nowrap;-o-text-overflow: ellipsis;text-overflow: ellipsis;}试着加一下这个CSS  
     
    我试了,不行呀
    =================
    有没有后台执行的,将截断的字符绑定到DataGrid中的?
      

  12.   

    CSS应该可以的
    .test1{display: block;width: 200px;overflow: hidden;white-space: nowrap;-o-text-overflow: ellipsis;text-overflow: ellipsis;}然后在你的代码里面给要绑定的地方指定class=test1width: 200px; 这个地方你要更改~   改成你要的长度~ 否则当然没有效果了
      

  13.   

    看看这个整个Copy出去 存成静态页面
      

  14.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta name="author" content="??, aka Realazy" /><meta name="copyright" content="?&#49892;piCreative Commonsj,http://www.creativecommons.cn/licenses/by-nc-sa/1.0/" /><title>gpCSSB</title><style type="text/css">* {margin: 0;padding: 0;}a {text-decoration: none;color: #df3a0e;}ul {width: 200px;margin: 40px auto;padding: 12px 4px 12px 24px;border: 1px solid #005fb0;background: #e0f1ff;}li {margin: 12px 0;width: 200px;}li a {display: block;width: 200px;overflow: hidden;white-space: nowrap;-o-text-overflow: ellipsis;text-overflow: ellipsis;}/* firefox only */li:not(p) { /* wtf is? pls let me know*/clear: both;}li:not(p) a {max-width: 170px;float: left;}li:not(p):after {content: "...";float: left;width: 25px;padding-left: 5px;color: #df3a0e;}</style></head><body><ul><li><a href="#" title="??">??</a></li><li><a href="#" title="11111111111111111111111111111111111">11111111111111111111111111111111111111111111111111111</a></li><li><a href="#" title="1111111111111111111111111111111111111111111111111111">1111111111111111111111111111111111111111111111111111</a></li>
    </ul></body> </html>
      

  15.   

    /// <summary>
            /// 按字节数截取字符串,中文算两个字节,全角字符算两个字节,大写字母也算两个字节
           /// </summary>
           /// <param name="strSrc">要处理的字符串</param>
           /// <param name="count">要保留的字节数</param>
           /// <param name="withPoint">设置截取后加不加省略号('...') 默认的传true</param>
           /// <returns></returns>
           public static string GetStrByByteCount(string strSrc,int count,bool withPoint)
           {
               Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
               char[] stringChar = strSrc.ToCharArray();
               StringBuilder sb = new StringBuilder();
               int nLength = 0;
               bool isCut = false;
               for (int i = 0; i < stringChar.Length; i++)
               {
                   if ((regex.IsMatch((stringChar[i]).ToString())) || (stringChar[i] > 65280 && stringChar[i] < 65375) || (stringChar[i] > 64 && stringChar[i] < 91))
                   {
                       nLength += 2;
                       if (nLength > count)
                       {
                           isCut = true;
                           break;
                       }
                       sb.Append(stringChar[i]);
                   }
                   else
                   {
                       nLength = nLength + 1;
                       if (nLength > count)
                       {
                           isCut = true;
                           break;
                       }
                       sb.Append(stringChar[i]);
                   }           }
               if (isCut)
               {
                   if (withPoint)
                   {
                       return sb.ToString() + "...";
                   }
                   else
                   {
                       return sb.ToString();
                   }
               }
               else
               {
                   return sb.ToString();
               }
           }这个是我用的。中文和大写的算两个字节,还算准确。
    前台绑定时加上这个方法就行了。
      

  16.   

    将对应字段设为模版列<asp:TemplateField HeaderText="名称" SortExpression="id">
                            <ItemTemplate><div class="SubContent" style="width:50px"><asp:Label ID="labName" runat="server" /></div></ItemTemplate>
                        </asp:TemplateField>
    .SubContent
    {
        text-overflow:ellipsis;
        overflow:hidden;
        white-space:nowrap;
        word-break:keep-all; 
    }/*超出固定长度则多余部分会显示...*/
      

  17.   

    以上的方法还是有点问题~   比如 w  w占的地方比较大  但是i占的很小 所以字母多了以后 对不齐
      

  18.   

    讲要截取的汉字列 设置为模板页<asp:TemplateField HeaderText="名称" SortExpression="id">
                            <ItemTemplatestyle="width:50px"><asp:Label ID="labName" runat="server" Text='<%#CTZ(Eval("绑定列名").ToString())%>' /></div></ItemTemplate>
                        </asp:TemplateField>后台
    public string CTZ(string content)
    {
        if(content.Length>XX)//你所设定的字符长度
        {
            content.Substring(0,XX) ;
            content+="....";
         }
         return content;
    }
    给分吧 LZ 
      

  19.   

    先写一个 字符串操作类 StringOperate.cs:
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;/// <summary>
    /// StringOperate 的摘要说明
    /// </summary>
    public class StringOperate
    {
    public StringOperate()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    }
        public void LeftSubstring(string str, int len)
        {    }
        public string CutString(string str, int len)
        {
            str = str.Trim();
            byte[] myByte = System.Text.Encoding.Default.GetBytes(str);
            if (myByte.Length > len)
            {
                string result = "";
                for (int i = 0; i < str.Length; i++)
                {
                    byte[] tempByte = System.Text.Encoding.Default.GetBytes(result);
                    if (tempByte.Length < len)
                    {
                        result += str.Substring(i, 1);
                    }
                    else
                    {
                        break;
                    }
                }
                return result + "...";
            }
            else
            {
                return str;
            }
        }
    }
    在需要控制字数的页面写一个方法:
    //控制字数
        public string ShowSubstring(string str, int number)
        {
            StringOperate newLiuYan = new StringOperate();
            return newLiuYan.CutString(str, number); //number 为设定的显示字数
        }在绑定时使用:
    <%# ShowSubstring(DataBinder.Eval(Container.DataItem, "content").ToString(),18)%>
    // 这里设为 18 个字
      

  20.   

    我给我的给你
    <asp:Label ID="lbl_Title" runat="server" Text='<%# (Eval("N_Title").ToString()).Length>21 ?((Eval("N_Title")).ToString()).Substring(0,21)+"...":Eval("N_Title") %>' ToolTip='<%# Eval("N_Title")%>'></asp:Label>大于21就那样了
      

  21.   

    http://blog.csdn.net/sendling/archive/2007/03/27/1543300.aspx
    新闻标题太长,截取字段,鼠标放上去显示全部
      

  22.   

    CSS似乎好一些,但是实际数据已经到了客户端,而用((字段).Length>20)?(字段).Substring(0,20)+"…":(字段)  并没有输出到客户端{这样就省了一些网络资源},哪个好自己选吧
      

  23.   

    length属性时,汉字和英文字符都算做一个,
    这样显示出来差别确实很大,这个怎样解决呢?
    比如 ... 和 我我我这两个长度一样,显示效果可差远了,字符越多,差距越大啊.
      

  24.   

    #region 按字符串实际长度截取定长字符窜
            /// <summary>
            /// 按字符串实际长度截取定长字符窜
            /// </summary>
            /// <param name="str">原字符串</param>
            /// <param name="length">要截取的长度</param>
            /// <returns>string型字符串</returns>
            public string CutString(string str, int length)
            {
                string delsqace = str.Trim();
                int i = 0, j = 0;
                foreach (char chr in delsqace)
                {
                    if ((int)chr > 127)
                    {
                        i += 2;
                    }
                    else
                    {
                        i++;
                    }
                    if (i > length)
                    {
                        delsqace = delsqace.Substring(0, j);
                        break;
                    }
                    j++;
                }
                return delsqace;        }
            #endregion
    这个就不会出现中文和英文差别较大的了
      

  25.   

    最初想到的办法是数据绑定的时候做个处理
    css这个值得学习
      

  26.   

    还可以用CSS定义啊 长见识啊
      

  27.   

    /// <summary>
        /// 字符串处理静态类
        /// </summary>
        public static class StringMangeCust
        {
            /// <summary>
            /// 获取该字符串的字节数,按照英文一个字节,中文两个字节计算
            /// </summary>
            /// <param name="SourStr">需要计算的字符串</param>
            /// <returns>字节数</returns>
            public static int GetStringByteCount(string SourStr)
            {
                return  System.Text.Encoding.Default.GetByteCount(SourStr);
                
            }
            /// <summary>
            /// 返回一个字符串中的某个子字符串。
            /// </summary>
            /// <param name="SourStr">源字符串</param>
            /// <param name="StrIndex">起始位置</param>
            /// <param name="StrCount">子字符串的大小</param>
            /// <returns></returns>
            public static string SubString(string SourStr,int StrIndex, int StrCount)
            {
                byte[] TempBytes = System.Text.Encoding.Default.GetBytes(SourStr);
                string Result = "";
                int TempIndex = StrIndex;
                int TempCount = StrCount;
                int TempPlace=0;
                if ((StrCount > 0) & (TempBytes.Length >= StrIndex))
                {
                    while (TempPlace < TempBytes.Length)
                    {
                        if (TempBytes[TempPlace] > 127)
                        {
                            if (TempPlace == (TempIndex - 1))
                            {
                                TempIndex = TempIndex + 1;
                                TempPlace = TempPlace + 3;
                            }
                            else
                            {
                                TempPlace = TempPlace + 2;
                            }
                            if (TempPlace == (TempCount - 1))
                            {
                                TempPlace = TempPlace + 2;
                                TempCount = TempCount - 1;
                            }
                        }
                        else
                        {
                            TempPlace = TempPlace + 1;
                        }
                    }
                    if ((TempCount > 0) & (TempBytes.Length > StrIndex) & (TempBytes.Length>=(StrIndex+TempCount)))
                    {
                        Result = System.Text.Encoding.Default.GetString(TempBytes, StrIndex, StrCount);
                    }
                }
                return Result;
            }
            /// <summary>
            /// 按照子字符串的大小获取一个子字符串。
            /// </summary>
            /// <param name="SourStr">源字符串</param>
            /// <param name="StrCount">子字符串的大小</param>
            /// <returns></returns>
            public static string SubString(string SourStr, int StrCount)
            {
                return SubString(SourStr, 0, StrCount);
            }        /// <summary>
            /// 获得16位MD5加密后的字符串。
            /// </summary>
            /// <param name="SourStr">需要加密的字符串</param>
            /// <returns>加密后的md5字符串,用HEX格式显示</returns>
            public static string getMd5Str(string SourStr)
            {
                MD5 m5Obj = MD5.Create();
                string Result = "";
                byte[] TempBytes = m5Obj.ComputeHash(Encoding.UTF8.GetBytes(SourStr));
                foreach (Byte TempByte in TempBytes)
                {
                    Result = Result + TempByte.ToString("X2");
                }
                return Result;
            }        /// <summary>
            /// 获取一个层
            /// </summary>
            /// <param name="Left">层的left</param>
            /// <param name="Top">曾的top</param>
            /// <returns>返回层的html代码</returns>
            public static string Ceate(string Left, string Top)
            {
                string Result = "";
                Result="<div id=\"Layer1\" style=\"position:absolute; width:200px; height:115px; z-index:1\"></div>";
                return Result;
            }
            /// <summary>
            /// 判断一个字符串是否为实数,是实数返回true,否则返回false
            /// </summary>
            /// <param name="SourStr">字符串</param>
            /// <returns>是实数返回true,否则返回false</returns>
            public static bool CheckStrToNumberic(string SourStr)
            {
                if (SourStr.Trim() == "") return true;
                try
                {
                    Convert.ToDouble(SourStr);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            /// <summary>
            /// 取字符串中第一个圆括号括起来的数字
            /// </summary>
            /// <param name="SourString">字符串</param>
            /// <returns></returns>
            public static string SubStringID(String SourString)
            {
                if (SourString.IndexOf(")") > 0)
                {
                    return SourString.Substring(1, SourString.IndexOf(")") - 1);
                }
                else
                {
                    return "-1";
                }
            }
        }
      

  28.   

    直接在select语句里
    select substring(title,1,20) from 表名
    读出来后加...
      

  29.   

    用Css控制 是最好的方法。style="width:100px;text-overflow:ellipsis;overflow:hidden;table-layout:fixed;WHITE-SPACE: nowrap;"需要改变长度就把 width调一下。