String.Replace ignoring case

前端 未结 17 643
野趣味
野趣味 2020-11-29 19:03

I have a string called \"hello world\"

I need to replace the word \"world\" to \"csharp\"

for this I use:

string.Replace(\"World\", \"csharp\         


        
相关标签:
17条回答
  • 2020-11-29 19:54

    Lots of suggestions using Regex. How about this extension method without it:

    public static string Replace(this string str, string old, string @new, StringComparison comparison)
    {
        @new = @new ?? "";
        if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(old) || old.Equals(@new, comparison))
            return str;
        int foundAt = 0;
        while ((foundAt = str.IndexOf(old, foundAt, comparison)) != -1)
        {
            str = str.Remove(foundAt, old.Length).Insert(foundAt, @new);
            foundAt += @new.Length;
        }
        return str;
    }
    
    0 讨论(0)
  • 2020-11-29 19:54

    Using @Georgy Batalov solution I had a problem when using the following example

    string original = "blah,DC=bleh,DC=blih,DC=bloh,DC=com"; string replaced = original.ReplaceIgnoreCase(",DC=", ".")

    Below is how I rewrote his extension

    public static string ReplaceIgnoreCase(this string source, string oldVale, 
    string newVale)
        {
            if (source.IsNullOrEmpty() || oldVale.IsNullOrEmpty())
                return source;
    
            var stringBuilder = new StringBuilder();
            string result = source;
    
            int index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
            bool initialRun = true;
    
            while (index >= 0)
            {
                string substr = result.Substring(0, index);
                substr = substr + newVale;
                result = result.Remove(0, index);
                result = result.Remove(0, oldVale.Length);
    
                stringBuilder.Append(substr);
    
                index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
            }
    
            if (result.Length > 0)
            {
                stringBuilder.Append(result);
            }
    
            return stringBuilder.ToString();
        }
    
    0 讨论(0)
  • 2020-11-29 20:00

    You can also try the Regex class.

    var regex = new Regex( "camel", RegexOptions.IgnoreCase ); var newSentence = regex.Replace( sentence, "horse" );

    0 讨论(0)
  • 2020-11-29 20:00

    .Net Core has this method built-in: Replace(String, String, StringComparison) Doc. Now we can simply write: "...".Replace("oldValue", "newValue", StringComparison.OrdinalIgnoreCase)

    0 讨论(0)
  • 2020-11-29 20:01

    (Edited: wasn't aware of the `naked link' problem, sorry about that)

    Taken from here:

    string myString = "find Me and replace ME";
    string strReplace = "me";
    myString = Regex.Replace(myString, "me", strReplace, RegexOptions.IgnoreCase);
    

    Seems you are not the first to complain of the lack of case insensitive string.Replace.

    0 讨论(0)
提交回复
热议问题