textparser.cs

来自「ASP.NET的一些开发实例,有论坛管理系统等」· CS 代码 · 共 49 行

CS
49
字号
using System;
using System.Text;
using System.IO;

namespace Service
{
	public class TextParser
	{
		public TextParser()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		//Method to parse Text into HTML
		public static 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
			return sw.GetStringBuilder().ToString();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?