String.Replace ignoring case

前端 未结 17 773
野趣味
野趣味 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:41

    Doesn't this work: I cant imaging anything else being much quicker or easier.

    public static class ExtensionMethodsString
    {
        public static string Replace(this String thisString, string oldValue, string newValue, StringComparison stringComparison)
        {
            string working = thisString;
            int index = working.IndexOf(oldValue, stringComparison);
            while (index != -1)
            {
                working = working.Remove(index, oldValue.Length);
                working = working.Insert(index, newValue);
                index = index + newValue.Length;
                index = working.IndexOf(oldValue, index, stringComparison);
            }
            return working;
        }
    }
    

提交回复
热议问题