.NEt专家博客!
asp.net很有用的字符串操作类,值得收藏
上一篇 /
下一篇 2008-10-08 16:26:51
/ 个人分类:WEB开发
using System; !U7N!nJ%F/_-e5uq]G0using System.Collections.Generic; 2V| vp
t0using System.Text; 1r:z].bw0using System.Collections; R#?6y3o X0using System.Text.RegularExpressions;ITPUB个人空间Rlao0X}(c0]z!ts*sk using System.Security.Cryptography; d9d|(}2sJW0/**/ _gg ]`y[j!b0//////////////////////////////////////////////////// U FLyYa(sn \0///功能:字符文本操作类ITPUB个人空间 ]%E nXU ///程序:郭城ITPUB个人空间o.aL^u9v+\y ///ITPUB个人空间:v?8} Md9l2U /// #Fd|6|M9^_|l4n
bi0//////////////////////////////////////////////////// Dao:}t0namespace XHW )F(Y)x1SKG+g`0{ITPUB个人空间B1S-KK8O{ /// <summary>ITPUB个人空间t]8~[qfS /// 字符文本操作类 &KuiC:|*u@2c
W$k"q0 /// </summary> [%ZD?&D?0 public class StringHelperITPUB个人空间dpA*^x+tr}mV { ;H8wF0jl-s5U0 public static bool IsContains(string[] strs, string value) D` aC$Jc,h+_Y0 { SDOMB0W3xF0 if (strs == null) 5Y0k\Yj0 {ITPUB个人空间,s?*TQT&aA:oUM~ return false;ITPUB个人空间2f,H
@em1D~gZ3H } foreach (string str in strs)ITPUB个人空间Z}/K3}'n/@,o+{T { ;K%t[r'TY~0 if (str == value) 9?QG]wS U/R0 {ITPUB个人空间o}ZofS return true;ITPUB个人空间7S,y7TE7si[8r }ITPUB个人空间*li6q/hQ7B)DN } return false;
?&t|Lb$H0wa0 } #region 字符串过滤 #region 对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码ITPUB个人空间i`WUjy /**/ITPUB个人空间fa)| fy ])W /// <summary> ;a/MD0K*il0 /// 对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码 &@U+j%X8TUd I0 /// </summary>ITPUB个人空间[,?'Od2o4LR
wf /// <param name="source"></param>
k?+lZ(ZA(k6U0 /// <returns></returns>ITPUB个人空间Ux3HO2g#D!n9o #endregion D2gnmQ4n0 public static string EncodeToHtml(string source)ITPUB个人空间!K+XO3T^J { w f#Q)WpWc
fO0 source = source.Trim(); *^,lE(j(_0Q0 source = source.Replace("'", "''"); ~#m4lEb2o
iF1I0 source = source.Replace("\\", "\");ITPUB个人空间yOL9@
J/I*{1vB"i source = System.Web.HttpContext.Current.Server.HtmlEncode(source); o+y Sw`A%?0 source = source.Replace("\r\n", "<br>"); M.pK+}$Eif0 source = source.Replace("\n", "<br>");ITPUB个人空间p7G*bf0K*|6R-xR+M return source;ITPUB个人空间i?$C @ a
R } Pm e(vf0mLO0 #region [否决的]对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码 M@EY;q:JO0 /**/ITPUB个人空间 p}7lp)G /// <summary>ITPUB个人空间'O0|5X_Tq#MV m$P /// [否决的]对字符串进行HTML编码,针对(input,Textarea)输入时过滤脚本及HTML编码 (不符合命名规范,请使用 EncodeToHtml 方法 ) /?#b$K3o#K)Qd5N0 /// </summary>ITPUB个人空间a B:W#Ex5FLOPo /// <param name="source"></param>ITPUB个人空间f3_e:V/G7Z /// <returns></returns> t(|~:Nn/N0 #endregion k'S2e-Q@.G,d$z0 public static string HtmlFilterForInput(string source) .Q/]PJ CA3d0 { 9YLz?HYlB0 return EncodeToHtml(source);ITPUB个人空间SWN(E1mq"d:k X^ }
ITPUB个人空间8wn W+hEP #region 还原HTML编码为字符串,还原HTML编码为字符串,用于返回到input或 Textarea 输入框
|