现在我要截取字符串 
<font   color= "red "> 这里是内容 </font>
怎么才能去只截取内容 而不截取格式呢 
就算不保留格式也行

解决方案 »

  1.   

    后台写个截取方法 public string getSubString(string title, int strlength)
            {
                if (stringToSub == null || stringToSub.Length == 0 || length <= 0) return "";
                Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
                char[] stringChar = stringToSub.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()))
                    {
                        sb.Append(stringChar[i]);
                        nLength += 2;
                    }
                    else
                    {
                        sb.Append(stringChar[i]);
                        nLength = nLength + 1;
                    }                if (nLength > length)
                    {
                        isCut = true;
                        break;
                    }
                }
                if (isCut)
                    return sb.ToString() + "...";
                else
                    return sb.ToString();
            }这是我的方法,可能实际开发中LZ不需要写这么复杂,直接判断长度,如果长度超出截取就可以了<%#getSubString("这里是内容",40)%>
      

  2.   

    用js弄就很简单了!
    <div id="aa"><font color= "red "> 这里是内容 </font></div>document.getElementById("aa").innerText
      

  3.   


    <font color= "red "> 这里是内容 </font>开始截取的位置是第一次发现“>”,结束位置是第二次发现“<”
      

  4.   

    <div id="aa"><font color= "red "> 这里是内容 </font></div>document.getElementById("aa").innerText
    up
      

  5.   

    很简单。用jquery一句话.<font color= "red "> 这里是内容 </font>
    <script src="App_common/scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function(){
                $("font[color='red']").val();//输出结果:这里是内容
            });
        </script>
      

  6.   

    我这些字符串是放在datalist里面的
    还有就是我想在后台截取
      

  7.   

    先去标签 再截str = Regex.Replace(str, @"<[^<>]*>", "");
    str = str.SubString(...);
      

  8.   

    <input id="hidvar" type="hidd***" ruant="server">
    var  Varfont=$("font[color=red]").val();
    $("#hidvar").val(Varfont);
    后台:string ss=this.hidvar.value;
      

  9.   

    public string getSubString(string title, int strlength)
            {
                if (stringToSub == null || stringToSub.Length == 0 || length <= 0) return "";
                Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
                char[] stringChar = stringToSub.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()))
                    {
                        sb.Append(stringChar[i]);
                        nLength += 2;
                    }
                    else
                    {
                        sb.Append(stringChar[i]);
                        nLength = nLength + 1;
                    }
      

  10.   

    屏蔽 html标签 调用一下就好了 祝你好运! public static string NoHTML(string Htmlstring)
        {
            string strOutput = Htmlstring;        strOutput = Regex.Replace(strOutput, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            // strOutput = Regex.Replace(strOutput,@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>","",RegexOptions.IgnoreCase);//在个别情况下会引起IIS吃爆CPU的现象
            strOutput = Regex.Replace(strOutput, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"-->", "", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"<!--.*", "", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            strOutput = Regex.Replace(strOutput, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");
            strOutput = HttpContext.Current.Server.HtmlEncode(strOutput).Trim();
            strOutput=strOutput.Replace(" ", "");
            return strOutput;
        }