C# 如何创建公共字符串替换为4个方法

C# 如何创建公共字符串替换为4个方法,c#,C#,在.NET Core 2.0、.NET Core 3.1中,有一个包含四个重载的字符串。我有一个类库项目,需要在其中使用替换字符串。不幸的是,我的目标框架是4.6,系统字符串似乎没有“String.Replace”和下面代码中的四个重载。有没有办法在Visual Studio 2015 C#的扩展中使用替换字符串 我只能从这个网站上收集其中一种方法,而无法找出其他三种方法。如蒙协助,将不胜感激 // // Parameters: // oldValue:

在.NET Core 2.0、.NET Core 3.1中,有一个包含四个重载的字符串。我有一个类库项目,需要在其中使用替换字符串。不幸的是,我的目标框架是4.6,系统字符串似乎没有“String.Replace”和下面代码中的四个重载。有没有办法在Visual Studio 2015 C#的扩展中使用替换字符串

我只能从这个网站上收集其中一种方法,而无法找出其他三种方法。如蒙协助,将不胜感激

        //
    // Parameters:
    //   oldValue:
    //
    //   newValue:
    //
    //   ignoreCase:
    //
    //   culture:
    public String Replace(String oldValue, String newValue, bool ignoreCase, CultureInfo culture);
    //
    // Parameters:
    //   oldValue:
    //
    //   newValue:
    //
    //   comparisonType:
    public String Replace(String oldValue, String newValue, StringComparison comparisonType);
    //
    // Summary:
    //     Returns a new string in which all occurrences of a specified string in the current
    //     instance are replaced with another specified string.
    //
    // Parameters:
    //   oldValue:
    //     The string to be replaced.
    //
    //   newValue:
    //     The string to replace all occurrences of oldValue.
    //
    // Returns:
    //     A string that is equivalent to the current string except that all instances of
    //     oldValue are replaced with newValue. If oldValue is not found in the current
    //     instance, the method returns the current instance unchanged.
    //
    // Exceptions:
    //   T:System.ArgumentNullException:
    //     oldValue is null.
    //
    //   T:System.ArgumentException:
    //     oldValue is the empty string ("").
    public String Replace(String oldValue, String newValue);
    //
    // Summary:
    //     Returns a new string in which all occurrences of a specified Unicode character
    //     in this instance are replaced with another specified Unicode character.
    //
    // Parameters:
    //   oldChar:
    //     The Unicode character to be replaced.
    //
    //   newChar:
    //     The Unicode character to replace all occurrences of oldChar.
    //
    // Returns:
    //     A string that is equivalent to this instance except that all instances of oldChar
    //     are replaced with newChar. If oldChar is not found in the current instance, the
    //     method returns the current instance unchanged.
    public String Replace(char oldChar, char newChar);
谢谢。

正确,不同于

您可以使用自己的扩展方法添加缺少的函数,如中所述

一个好的起点是检查中缺少的方法的实现

因此,您应该能够实现您需要的缺少的方法

Udate:

让我们看看您在SharpHash和:

看起来此操作的目标只是使用
ignoreCase
正确处理十六进制输入,在这种情况下需要设置区域性,但可以忽略。知道了这一点,我们可以提供一个简单的替代实现,作为使用regex功能的扩展方法:

public static class StringExtensions
{
    public static string Replace(this string str, string oldValue, string newValue,
        bool ignoreCase, CultureInfo culture)
    {
       return Regex.Replace(str, oldValue, newValue,
           ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None);
    }
}
请注意,此实现不完全替换原始方法,因为

  • 字符串中的正则表达式语言元素
  • 演出
  • 培养

没有观察到这些问题。但是对于你的问题,解决方案应该足够了。如果没有,请尝试根据需要扩展和修改解决方案。

您可以自己创建扩展方法,以同样的方式使用。谢谢您的回复。我看了Fruchtzwerg提供的东西。然而,我认为把这个扩展方法放在一起有点让我不知所措。谢谢你的链接。我试图将这些方法复制到一个类中。使课程成为部分课程,但我有很多事情无法解决,因为我在这方面的知识不多;类型必须不可为Null,参数3无法转换,当前上下文中不存在GetCaseComareofComparisonCulture,不变量不包含定义,字符串扩展不包含定义,等等。不确定是否真正理解如何创建此方法。您需要哪个重载缺少的实现?这两个字符串都是?这个字符串替换来自一个哈希项目“h**ps://github.com/ron4fun/SharpHash”,这是一个更老的Utils;c类;第285行和第290行。这个项目在visual studio 2019中,我想我有visual studio 2015。非常感谢Fruchtzwerg。我想这就成功了。我感谢你的帮助。
public static class StringExtensions
{
    public static string Replace(this string str, string oldValue, string newValue,
        bool ignoreCase, CultureInfo culture)
    {
       return Regex.Replace(str, oldValue, newValue,
           ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None);
    }
}