日期:2014-05-18  浏览次数:20836 次

为什么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(), ",");