例如我有一篇文章,是用freeTextBox录入的。大概有1000个字左右,我想在主页上显示文章的一部分个字。就像这样。“testtesttesttesttesttesttest
testtesttesttesttesttesttest...”后面多的字符就用“”代替另外的字。请教各位大侠,该怎么弄?我用过截取字符串,但是freeTextBox生成的数据是html格式的。如果截取不完整标签将会显示错误。我想不到什么办法,求教啊。

解决方案 »

  1.   

    只能通过判断去判断,截取位置之前存在“<”,就必须向后延几位,直到碰到“>”,为止
      

  2.   

    通过正过滤html字符
    public static string InputText(string text, int maxLength) 
        { 
            text = Regex.Replace(text, "[\\s]{2,}", " "); 
            text = Regex.Replace(text, "( <[b|B][r|R]/*>)+|( <[p|P](.|\\n)*?>)", "\n"); 
            text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); 
            text = Regex.Replace(text, " <(.|\\n)*?>", string.Empty); 
            text = text.Replace("'", "''"); 
            return text; 
        }
      public static string subText(string text, int maxLength)
            {
                text = text.Trim();
                if (string.IsNullOrEmpty(text))
                    return string.Empty;
                if (maxLength > 0)
                {
                    if (text.Length > maxLength)
                        text = text.Substring(0, maxLength);
                }
                text = text.Replace("'", "''");
                return text;
            }
      

  3.   

    str--你传的值
    n--显示的字符数
    public static string CutStr(string str, int n)
            {
                string temp = string.Empty;
                if (string.IsNullOrEmpty(str))
                {
                    return "暂无";
                }
                else if (System.Text.Encoding.Default.GetByteCount(str) <= n)//如果长度比需要的长度n小,返回原字符串
                {
                    return str;
                }
                else
                {
                    int t = 0;
                    char[] q = str.ToCharArray();
                    for (int i = 0; i < q.Length && t < n; i++)
                    {
                        if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5)//是否汉字
                        {
                            temp += q[i];
                            t += 2;
                        }
                        else
                        {
                            temp += q[i];
                            t++;
                        }
                    }
                    return (temp) + "..";
                }
            }
      

  4.   

    http://blog.csdn.net/ChaoYang0502/archive/2009/01/05/3716937.aspx
      

  5.   

    http://blog.csdn.net/xianfajushi/archive/2009/11/27/4734699.aspx