首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

为什么replace无法替换()

2012-03-09 
为什么replace无法替换(在线等)写了一个函数private string Injection(string wd){string InjeTxt sele

为什么replace无法替换(在线等)
写了一个函数
 private string Injection(string wd)
  {
  string InjeTxt = "select|update|insert|delete|declare|@|exec|=|<|>";
  string[] InjeTxtArr;
  InjeTxtArr = InjeTxt.Split('|');
  for (int i = 0; i < InjeTxtArr.Length; i++)
  {
  wd.Replace(InjeTxtArr[i].ToString(), ",");
  }
  return wd;
  }

调用的时候传进去"selectdwef"
返回的仍然是"selectdwef"
这是什么问题啊!

[解决办法]
Replace的结果是一个新的字符串,而不是对原串的修改,因此代码应如下:

C# code
private string Injection(string wd){    string InjeTxt = "select|update |insert |delete |declare |@ |exec |= |< |> ";    string[] InjeTxtArr;    InjeTxtArr = InjeTxt.Split('|');    for (int i = 0; i < InjeTxtArr.Length; i++)    {        wd = wd.Replace(InjeTxtArr[i].ToString(), ",");    }    return wd;}
[解决办法]
wd = wd.Replace(InjeTxtArr[i].ToString(), ","); 

热点排行