文本添加格式转换为html类!

using System;
using System.Text;
using System.IO;

namespace WebSite.Support
{
 public class TextParser
 {
  public TextParser()
  {
   //
   // TODO: Add constructor logic here
   //
  }

  //Method to parse Text into HTML
  public string Parser(string text, bool allow)
  {
   //Create a StringBuilder object from the string intput parameter
   StringBuilder sb = new StringBuilder(text) ;
   //Replace all double white spaces with a single white space and  
   sb.Replace(" "," ");
   //Check if HTML tags are not allowed
   if(!allow)
   {
    //Convert the brackets into HTML equivalents
    sb.Replace("<","&lt;") ;
    sb.Replace(">","&gt;") ;
    //Convert the double quote
    sb.Replace("/"","&quot;");
   }
   //Create a StringReader from the processed string of the StringBuilder object
   StringReader sr = new StringReader(sb.ToString());
   StringWriter sw = new StringWriter();
   //Loop while next character exists
   while(sr.Peek()>-1)
   {
    //Read a line from the string and store it to a temp variable
    string temp = sr.ReadLine();
    //write the string with the HTML break tag
    //Note here write method writes to a Internal StringBuilder object created automatically
    sw.Write(temp+"<br>") ;
   }
   //Return the final processed text
   string temp="<html><body>"+sw.GetStringBuilder().ToString()+"</body></html>";
   return temp;
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值