replace忽略大小写

replace忽略大小写

本文介绍了与string.replace忽略大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Hello World的字符串

I have a string called "hello world"

我需要替换的单词世界到csharp的

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

为了这个,我使用:

string.Replace("World", "csharp");

但作为一个结果,我没有得到的字符串替换。原因是案件敏感。原字符串中包含世界,而我试图取代世界。

but as a result, I don't get the string replaced. The reason is case sensitiveness. The original string contains "world" whereas I'm trying to replace "World".

有什么办法,以避免在与string.replace方法,这种情况下,敏感?

Is there any way to avoid this case sensitiveness in string.Replace method?

推荐答案

您可以使用的并执行不区分大小写的替换:

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"
    }
}

这篇关于与string.replace忽略大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:23