String.Replace ignoring case

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

    You could use a Regex and perform a case insensitive replace:

    class Program
    {
        static void Main()
        {
            string input = "hello WoRlD";
            string result = 
               Regex.Replace(input, "world", "csharp", RegexOptions.IgnoreCase);
            Console.WriteLine(result); // prints "hello csharp"
        }
    }
    

提交回复
热议问题