×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

try this, I used C#2.0, but easy to switch

string html = @"<ll>abcdefg</ll> <pp>1234567</pp>";
bool isValid = true;
List<byte> buffer = new List<byte>();
foreach (byte x in html.ToCharArray())
{
if (x == '<')
{
isValid = false;
}
if (isValid == true)
{
buffer.Add(x);
}
if (x == '>')
{
isValid = true;
}
}

string cleanString = Encoding.ASCII.GetString(buffer.ToArray());
MessageBox.Show(cleanString);
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / 有一些HTML Code,想去掉里面的HTML tag,只要留文本, 用c#+.net 1.1怎么写。谢谢。
    • try this in c#:
      string strHtmlCode = "<html><body width=123>test</body></html>";
      Regex r = new Regex("<[^<>]+>");
      foreach(Match m in r.Matches(strHtmlCode))
      strHtmlCode = strHtmlCode.Replace(m.ToString(),"");
      Response.Write(strHtmlCode);


      A good site about regex:
      http://www.regular-expressions.info/quickstart.html
      • not only about regex, how about this one "<body>Professor wrote, <b>0<1, 2>1</b></body>"
        • 这个能正确显示吗?要是&lt;&gt;才行吧?
        • Good point. So the user need define a better regular expression.
    • try this, I used C#2.0, but easy to switch
      string html = @"<ll>abcdefg</ll> <pp>1234567</pp>";
      bool isValid = true;
      List<byte> buffer = new List<byte>();
      foreach (byte x in html.ToCharArray())
      {
      if (x == '<')
      {
      isValid = false;
      }
      if (isValid == true)
      {
      buffer.Add(x);
      }
      if (x == '>')
      {
      isValid = true;
      }
      }

      string cleanString = Encoding.ASCII.GetString(buffer.ToArray());
      MessageBox.Show(cleanString);