Is there a case insensitive string replace in .Net without using Regex?

纵饮孤独 提交于 2019-12-28 19:36:12

问题


I recently had to perform some string replacements in .net and found myself developing a regular expression replacement function for this purpose. After getting it to work I couldn't help but think there must be a built in case insensitive replacement operation in .Net that I'm missing?

Surely when there are so many other string operations that support case insensitive comparission such as;

var compareStrings  = String.Compare("a", "b", blIgnoreCase);
var equalStrings    = String.Equals("a", "b", StringComparison.CurrentCultureIgnoreCase);

then there must be a built in equivalent for replace?


回答1:


Found one in the comments here: http://www.codeproject.com/Messages/1835929/this-one-is-even-faster-and-more-flexible-modified.aspx

static public string Replace(string original, string pattern, string replacement, StringComparison comparisonType)
{
     return Replace(original, pattern, replacement, comparisonType, -1);
}

static public string Replace(string original, string pattern, string replacement, StringComparison comparisonType, int stringBuilderInitialSize)
{
     if (original == null)
     {
         return null;
     }

     if (String.IsNullOrEmpty(pattern))
     {
         return original;
     }


     int posCurrent = 0;
     int lenPattern = pattern.Length;
     int idxNext = original.IndexOf(pattern, comparisonType);
     StringBuilder result = new StringBuilder(stringBuilderInitialSize < 0 ? Math.Min(4096, original.Length) : stringBuilderInitialSize);

     while (idxNext >= 0)
     {
        result.Append(original, posCurrent, idxNext - posCurrent);
        result.Append(replacement);

        posCurrent = idxNext + lenPattern;

        idxNext = original.IndexOf(pattern, posCurrent, comparisonType);
      }

      result.Append(original, posCurrent, original.Length - posCurrent);

      return result.ToString();
}

Should be the fastest, but i haven't checked.

Otherwise you should do what Simon suggested and use the VisualBasic Replace function. This is what i often do because of its case-insensitive capabilities.

string s = "SoftWare";
s = Microsoft.VisualBasic.Strings.Replace(s, "software", "hardware", 1, -1, Constants.vbTextCompare);

You have to add a reference to the Microsoft.VisualBasic dll.




回答2:


It's not ideal, but you can import Microsoft.VisualBasic and use Strings.Replace to do this. Otherwise I think it's case of rolling your own or stick with Regular Expressions.




回答3:


Here's an extension method. Not sure where I found it.

public static class StringExtensions
{
    public static string Replace(this string originalString, string oldValue, string newValue, StringComparison comparisonType)
    {
        int startIndex = 0;
        while (true)
        {
            startIndex = originalString.IndexOf(oldValue, startIndex, comparisonType);
            if (startIndex == -1)
                break;

            originalString = originalString.Substring(0, startIndex) + newValue + originalString.Substring(startIndex + oldValue.Length);

            startIndex += newValue.Length;
        }

        return originalString;
    }

}



回答4:


This is a VB.NET adaptation of rboarman's answer above with the necessary checks for null and empty strings to avoid an infinite loop.

Public Function Replace(ByVal originalString As String, 
                        ByVal oldValue As String, 
                        ByVal newValue As String, 
                        ByVal comparisonType As StringComparison) As String
    If Not String.IsNullOrEmpty(originalString) AndAlso 
       Not String.IsNullOrEmpty(oldValue) AndAlso 
       newValue IsNot Nothing Then
        Dim startIndex As Int32

        Do While True
            startIndex = originalString.IndexOf(oldValue, startIndex, comparisonType)
            If startIndex = -1 Then Exit Do
            originalString = originalString.Substring(0, startIndex) & newValue & 
                             originalString.Substring(startIndex + oldValue.Length)
            startIndex += newValue.Length
        Loop
    End If

    Return originalString
End Function



回答5:


My 2 cents:

public static string Replace(this string originalString, string oldValue, string newValue, StringComparison comparisonType)
{
    if (originalString == null)
        return null;
    if (oldValue == null)
        throw new ArgumentNullException("oldValue");
    if (oldValue == string.Empty)
        return originalString;
    if (newValue == null)
        throw new ArgumentNullException("newValue");

    const int indexNotFound = -1;
    int startIndex = 0, index = 0;
    while ((index = originalString.IndexOf(oldValue, startIndex, comparisonType)) != indexNotFound)
    {
        originalString = originalString.Substring(0, index) + newValue + originalString.Substring(index + oldValue.Length);
        startIndex = index + newValue.Length;
    }

    return originalString;
}



Replace("FOOBAR", "O", "za", StringComparison.OrdinalIgnoreCase);
// "FzazaBAR"

Replace("", "O", "za", StringComparison.OrdinalIgnoreCase);
// ""

Replace("FOO", "BAR", "", StringComparison.OrdinalIgnoreCase);
// "FOO"

Replace("FOO", "F", "", StringComparison.OrdinalIgnoreCase);
// "OO"

Replace("FOO", "", "BAR", StringComparison.OrdinalIgnoreCase);
// "FOO"



回答6:


Update in .NET Core 2.0+ (August 2017)

This is natively available in .NET Core 2.0+ with String.Replace which has the following overloads

public string Replace (string oldValue, string newValue, StringComparison comparisonType);
public string Replace (string oldValue, string newValue, bool ignoreCase, System.Globalization.CultureInfo culture);

PS: You can browse .NET Core source if you want to see how MS implemented it

So you can use either like this:

"A".Replace("a", "b", StringComparison.CurrentCultureIgnoreCase);
"A".Replace("a", "b", true, CultureInfo.CurrentCulture);

Legacy .NET Framework 4.8- option for VB Projects

Visual Basic has an Option Compare setting which can be set to Binary or Text

Setting to Text will make all string comparisons across your project case insensitive by default.

So, as other answers have suggested, if you are pulling in the Microsoft.VisualBasic.dll, when calling Strings.Replace if you don't explicitly pass a CompareMethod the method will actually defer to the Compare option for your file or project using the [OptionCompare] Parameter Attribute

So either of the following will also work (top option only available in VB , but both rely on VisualBasic.dll)

Option Compare Text

Replace("A","a","b")
Replace("A","a","b", Compare := CompareMethod.Text)



回答7:


I know of no canned instance in the framework, but here's another extension method version with a minimal amount of statements (although maybe not the fastest), for fun. More versions of replacement functions are posted at http://www.codeproject.com/KB/string/fastestcscaseinsstringrep.aspx and "Is there an alternative to string.Replace that is case-insensitive?" as well.

public static string ReplaceIgnoreCase(this string alterableString, string oldValue, string newValue){
    if(alterableString == null) return null;
    for(
        int i = alterableString.IndexOf(oldValue, System.StringComparison.CurrentCultureIgnoreCase);
        i > -1;
        i = alterableString.IndexOf(oldValue, i+newValue.Length, System.StringComparison.CurrentCultureIgnoreCase)
    ) alterableString =
        alterableString.Substring(0, i)
        +newValue
        +alterableString.Substring(i+oldValue.Length)
    ;
    return alterableString;
}



回答8:


You can use Microsoft.VisualBasic.Strings.Replace and pass in Microsoft.VisualBasic.CompareMethod.Text to do a case insensitive replace like this:

Dim myString As String = "One Two Three"
myString = Replace(myString, "two", "TWO", Compare:= CompareMethod.Text)


来源:https://stackoverflow.com/questions/5549426/is-there-a-case-insensitive-string-replace-in-net-without-using-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!