C# 转换用户输入的字符串

                #region 转换用户输入

		/// <summary>
		/// 将用户输入的字符串转换为可换行、替换Html编码、无危害数据库特殊字符、去掉首尾空白、的安全方便代码。
		/// </summary>
		/// <param name="inputString">用户输入字符串</param>
		public static string ConvertStr(string inputString)
		{
			string retVal=inputString;
			//retVal=retVal.Replace("&","&amp;"); 
			retVal=retVal.Replace("\"","&quot;"); 
			retVal=retVal.Replace("<","&lt;"); 
			retVal=retVal.Replace(">","&gt;"); 
			retVal=retVal.Replace(" ","&nbsp;"); 
			retVal=retVal.Replace("  ","&nbsp;&nbsp;"); 
			retVal=retVal.Replace("\t","&nbsp;&nbsp;");
			retVal=retVal.Replace("\r", "<br>");
			return retVal;
		}

		public static string InputText(string inputString)
		{
			string retVal=inputString;
			retVal= ConvertStr(retVal);
			retVal=retVal.Replace("[url]", "");
			retVal=retVal.Replace("[/url]", "");
			return retVal;
		}


        /// <summary>
        /// 将html代码显示在网页上
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
		public static string OutputText(string inputString)
		{
			string retVal=System.Web.HttpUtility.HtmlDecode(inputString);
			retVal=retVal.Replace("<br>","");
			retVal=retVal.Replace("&amp","&;"); 
			retVal=retVal.Replace("&quot;","\""); 
			retVal=retVal.Replace("&lt;","<"); 
			retVal=retVal.Replace("&gt;",">"); 
			retVal=retVal.Replace("&nbsp;"," "); 
			retVal=retVal.Replace("&nbsp;&nbsp;","  "); 
			return retVal;
		}

		public static string ToUrl(string inputString)
		{
			string retVal=inputString;
			retVal= ConvertStr(retVal);
			return Regex.Replace(retVal,@"\[url](?<x>[^\]]*)\[/url]",@"<a href=""$1"" target=""_blank"">$1</a>",RegexOptions.IgnoreCase);
		}

		public static string GetSafeCode(string str)
		{
			  str=str.Replace("'","");
			  str=str.Replace(char.Parse("34"),' ');
			  str=str.Replace(";","");
			return str;
		}

		#endregion

欢迎分享,(木庄网络博客交流QQ群:562366239

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论