String.Replace ignoring case

前端 未结 17 641
野趣味
野趣味 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"
        }
    }
    
    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 19:42

    Extensions make our lives easier:

    static public class StringExtensions
    {
        static public string ReplaceInsensitive(this string str, string from, string to)
        {
            str = Regex.Replace(str, from, to, RegexOptions.IgnoreCase);
            return str;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 19:43

    You can use the Microsoft.VisualBasic namespace to find this helper function:

    Replace(sourceString, "replacethis", "withthis", , , CompareMethod.Text)
    
    0 讨论(0)
  • 2020-11-29 19:47

    below is the alternative to replace string ignoring character case

    String thisString = "hello world"; 
    String replaceString = "World";
    
    //thisString.Replace("World", "csharp"); 
    //below is the alternative to replace string ignoring character case
    
    int start = StringUtils.indexOfIgnoreCase(thisString,replaceString);
    String searchKey = thisString.substring(start, start+replaceString.length());
    thisString= thisString.replaceAll(searchKey ,replaceString );
    System.out.println(thisString);
    
    //prints hello World
    
    0 讨论(0)
  • 2020-11-29 19:48

    I have wrote extension method:

    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);
    
            while (index >= 0)
            {
                if (index > 0)
                    stringBuilder.Append(result.Substring(0, index));
    
                if (newVale.IsNullOrEmpty().IsNot())
                    stringBuilder.Append(newVale);
    
                stringBuilder.Append(result.Substring(index + oldVale.Length));
    
                result = stringBuilder.ToString();
    
                index = result.IndexOf(oldVale, StringComparison.InvariantCultureIgnoreCase);
            }
    
            return result;
        }
    

    I use two additional extension methods for previous extension method:

        public static bool IsNullOrEmpty(this string value)
        {
            return string.IsNullOrEmpty(value);
        }
    
        public static bool IsNot(this bool val)
        {
            return val == false;
        }
    
    0 讨论(0)
提交回复
热议问题