我要在一个固定长度的区域显示字符串,如果字符串超过了这个宽度那么剩下的部分就由...代替,这看起来很容易解决,用如下方法就可以:
if(str.Length>10)
str=str.Substring(0,4)+"....";
但是问题来了,str中的字符可以是中文也可以是英文,
str1="中中中中中...";
str2="中aaaa...";
很明显str2和str1中的字符数是一样多,但是所占的宽度却不一样,如果这样排出来就不好看了,那么我应该采用什么方法,使得str1只显5个字符,str2显示出8个字符,那么这样看起来就整齐了!
str1="中中中中中...";
str2="中aaaaaaa...";

解决方案 »

  1.   

    我也在想这个问题,呵,高手回答一下啊====CSDN 小助手 V2.5 2005年11月05日发布====
    CSDN小助手是一款脱离浏览器也可以访问Csdn论坛的软件
    界面:http://blog.csdn.net/Qqwwee_Com/archive/2005/11/05/523395.aspx
    下载:http://szlawbook.com/csdnv2
      

  2.   

    轉換成byte然後固定byte的長度在轉換回來。
      

  3.   

    static void Main(string[] args)
    {
    Console.WriteLine(Class1.RetContent("aaaaaaa中中中中中中中中中中",10));
    }
              
                       /// <summary>
    /// Return split string
    /// </summary>
    /// <param name="strContent">content string</param>
    /// <param name="intSplitCount">split count(byte count)</param>
    /// <returns>split string</returns>
    public static string RetContent(string strContent, int intSplitCount)
    {
    int intTemp = 0;
    int i = 0;
    //regular expression of Chinese
    string strPat = @"[\u4e00-\u9fa5]";
    Regex reg = new Regex(strPat);
    string strRetContent = ""; if(reg.Replace(strContent.Trim(), "  ").Length <= intSplitCount)
    {
    return strContent.Trim();
    } while(intTemp < intSplitCount)
    {
    Match mh = reg.Match(strContent.Substring(i, 1));
    if(mh.Success)
    {
    intTemp++;
    intTemp++;
    }
    else
    {
    intTemp++;
    }
    strRetContent = strRetContent + strContent.Substring(i, 1);
    i++;
    } return strRetContent + "...";

    }
      

  4.   

    转自 http://birdshome.cnblogs.com/archive/2005/08/02/205401.html自动处理过长字符串显示的Web控件 
        我们很多时候需要在一行上显示一段说明文字,而由于Web页面宽度的不确定性,我们任意调节其宽度后,常常搞得文字撑出页面或者折成好多行。通过使用CSS,我们可以限制为一行的宽度,并使多余的字符隐藏。为了方便,做成一个小Web控件来使用。
        using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ComponentModel;namespace cnblogs.birdshome.WebControls
    {
        /**//// <summary>
        /// Summary description for AutoLabel.
        /// </summary>
        [DefaultProperty("Text"), 
            ToolboxData("<{0}:AutoLabel runat=server></{0}:AutoLabel>")]
        public class AutoLabel : System.Web.UI.WebControls.Label
        {
            protected override void CreateChildControls()
            {
                base.CreateChildControls ();
                this.Width = Unit.Percentage(100);
                this.Attributes["onmouseover"] = 
                     "if ( this.clientWidth < this.scrollWidth ) this.title = this.innerText; else this.title = '';";
                this.Attributes.CssStyle["white-space"] = "nowrap";
                this.Attributes.CssStyle["overflow"] = "hidden";
                this.Attributes.CssStyle["text-overflow"] = "ellipsis";
            }
        }
    }
        AutoLabel继承至Label控件,默认宽度为"100%",当把AutoLabel放入容器类元素中后,其内容的宽度受容器大小自动调整。并且当AutoLabel出现"..."号后,鼠标放在上面,其ToolTip会自动显示器完整内容。
      

  5.   

    public void BindGrid()
      {
       OleDbConnection myConnection = cn;
       DataSet ds  = new DataSet();
       OleDbDataAdapter adapter  = new OleDbDataAdapter("Select Title,CreateDate from article", myConnection);
       adapter.Fill(ds, "Document");
    //这里开始
       for(int i=0;i<ds.Tables[0].Rows.Count;i++)
       {
        if(ds.Tables[0].Rows[i]["Title"].ToString().Length>6)
         ds.Tables[0].Rows[i]["Title"]=CutString(ds.Tables[0].Rows[i]["Title"].ToString(),6);
       }
       MyDataGrid.DataSource = ds.Tables["Document"].DefaultView;
                MyDataGrid.DataBind();
       ShowStats();
      }#region 字符串截取函数
      public static string CutString(string inputString,int len)
      {
       ASCIIEncoding ascii =  new ASCIIEncoding();
       int tempLen=0;
       string tempString="";
       byte[] s = ascii.GetBytes(inputString);
       for(int i=0;i<s.Length;i++)
       {
        if((int)s[i]==63)
        {
         tempLen+=2;
        }
        else
        {
         tempLen+=1;
        }
                    
        try
        {
         tempString+=inputString.Substring(i,1);
        }
        catch
        {
         break;
        }    if(tempLen>len)
         break;
       }
       //如果截过则加上半个省略号
       byte[] mybyte=System.Text.Encoding.Default.GetBytes(inputString);
       if(mybyte.Length>len)
        tempString+="…";
       return tempString;
      }
      #endregion具体看:
    博客园 
    [原创]读出数据库数据,控制显示长度! 
    http://kary.cnblogs.com/archive/2004/08/16/33830.html
    玻璃箱
    http://web.mblogger.cn/taye/posts/24693.aspx