洛客
追求完美的用户体验。
随笔- 75  文章- 3  评论- 65 
博客园  首页  新随笔  联系  管理  订阅 订阅

.net中让LABEL接受的文本自动分行并且过滤多余的字符或者恶意字符

lbnr.Text=ToUrl(dr["Remark"].ToString());//具体到某个控件的text显示的内容引用


/// <summary>
  /// 让LABEL接受的文本自动分行并且过滤多余的字符或者恶意字符
  /// </summary>
  /// <param name="inputString"></param>
  /// <returns></returns>
  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;");//过滤字符\t
   retVal=retVal.Replace("\r", "<br>");//过滤字符\r<br>
   return retVal;
  }
  public static string ToUrl(string inputString)//引用并替换
  {
   string retVal=inputString;
   retVal= ConvertStr(retVal);
   return retVal;
//   retVal= Regex.Replace(retVal,@"\[url](?<x>[^\]]*)\[/url]",@"<a href=""$1"" target=""_blank"">$1</a>",RegexOptions.IgnoreCase);
//   retVal= Regex.Replace(retVal,@"\[flash=(?<width>\d+),(?<height>\d+)](?<x>[^\]]*)\[/flash]",@"<embed src=""$3"" width=""${width}"" height=""${height}""></embed>",RegexOptions.IgnoreCase);
//   retVal= Regex.Replace(retVal,@"\[flash](?<x>[^\]]*)\[/flash]",@"<embed src=""$1""></embed>",RegexOptions.IgnoreCase);
//   return Regex.Replace(retVal,@"\[img](?<x>[^\]]*)\[/img]",@"<a href=""$1"" target=""_blank""><img src=""$1"" onload=""javascript:if(this.width>screen.width-220)this.width=screen.width-220"" border=1></a>",RegexOptions.IgnoreCase);
  }
  

posted @ 2007-06-15 08:10 洛客 阅读(184) 评论(0) 编辑 收藏
刷新评论刷新页面返回顶部
程序员问答社区,解决您的IT难题
博客园首页博问新闻闪存程序员招聘知识库
Copyright ©2012 洛客