首页 新闻 搜索 专区 学院

替换危险字符

0
悬赏园豆:10 [已关闭问题]
public static string LetterReplace(string txt)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (char a in txt)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch(a)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case '&lt;': a.ToString().Replace("&lt;", "&lt;"); break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case '&gt;': a.ToString().Replace("&gt;", "&gt;"); break;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp; //返回值<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>我做了个危险字符的替换方法&nbsp; 但是到 return 这一步的时候 我不知道么继续写了&nbsp; <BR>在return 后 该么办啊
yeyang的主页 yeyang | 菜鸟二级 | 园豆:418
提问于:2008-06-11 16:44
< >
分享
其他回答(6)
0
[code] public static string LetterReplace(string txt) { if(txt == null)return null; return txt.Replace("<","安全字符").Replace(">","安全字符"); } [/code]
玉开 | 园豆:8822 (大侠五级) | 2008-06-11 17:05
0
楼主,您的办法是不是效率太低了? 如果按照楼主自己现在的思路继续走下去的话,我觉得楼主应该设置一个变量用来存储要返回的值。参考如下: public static string LetterReplace(string txt) { string backTxt; foreach (char a in txt) { switch(a) { case '<': backTxt += a.ToString().Replace("<", "<"); break; case '>': backTxt += a.ToString().Replace(">", ">"); break; default: backTxt += a.ToString(); break; } } return backTxt;//返回值 }
致博腾远 | 园豆:1389 (小虾三级) | 2008-06-12 09:00
0
同意楼上的回答。
李永京 | 园豆:3114 (老鸟四级) | 2008-06-13 12:03
0
public static string LetterReplace(string txt) { string backTxt; foreach (char a in txt) { switch(a) { case '<': backTxt += a.ToString().Replace("<", "<"); break; case '>': backTxt += a.ToString().Replace(">", ">"); break; default: backTxt += a.ToString(); break; } } return backTxt; }
Jared.Nie | 园豆:1940 (小虾三级) | 2008-06-13 14:02
0
可以用正则表达式
DreamTrue | 园豆:215 (菜鸟二级) | 2008-06-13 15:41
0
效率底下这样,
jowo | 园豆:2834 (老鸟四级) | 2008-06-14 09:24
0
/// <summary> /// 检测是否有Sql危险字符 /// </summary> /// <param name="str">要判断字符串</param> /// <returns>判断结果</returns> public static bool IsSafeSqlString(string str) { return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"); } //增强清理 public static string InputText(string inputString, int maxLength) { StringBuilder retVal = new StringBuilder(); // 检查是否为空 if ((inputString != null) && (inputString != String.Empty)) { inputString = inputString.Trim(); //检查长度 if (inputString.Length > maxLength) inputString = inputString.Substring(0, maxLength); //替换危险字符 for (int i = 0; i < inputString.Length; i++) { switch (inputString[i]) { case '"': retVal.Append("&quot;"); break; case '<': retVal.Append("&lt;"); break; case '>': retVal.Append("&gt;"); break; default: retVal.Append(inputString[i]); break; } } retVal.Replace("'", " ");// 替换单引号 } return retVal.ToString(); }
zjy | 园豆:3194 (老鸟四级) | 2008-06-20 16:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册