【问题标题】:Regex.Replace and static context?Regex.Replace 和静态上下文?
【发布时间】:2010-10-08 07:40:14
【问题描述】:

我这里有这段代码:

private Func<string, string> RemoveSpecialChars = str => Regex.Replace(str, "[ ./\\-]");

由于静态上下文,它抱怨(无法在静态上下文中访问非静态方法 Replace)关于 Replace 的调用。怎么了?

谢谢:)

【问题讨论】:

    标签: c# static-methods


    【解决方案1】:

    Regex.Replace 的静态重载有不同的签名:

    public static string Replace(
        string input,
        string pattern,
        string replacement
    )
    

    您缺少replacement 参数

    【讨论】:

    • 啊……只见树木不见森林:D
    【解决方案2】:

    你需要使用Regex.Replace(input,pattern,replacement)的方法,the one you use不是静态的:

    private Func<string, string> RemoveSpecialChars = 
                           str => Regex.Replace(str, "[ ./\\-]", replacementString);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多