Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore case in String.replace

string sentence = "We know it contains 'camel' word."; // Camel can be in different cases: string s1 = "CAMEL"; string s2 = "CaMEL"; string s3 = "CAMeL"; // ... string s4 = "Camel"; // ... string s5 = "camel"; 

How to replace 'camel' in sentence with 'horse' despite of string.Replace doesn't support ignoreCase on left string?

like image 467
Xaqron Avatar asked May 17 '11 02:05

Xaqron


People also ask

Is Java string replace case-sensitive?

Case insensitively replaces all occurrences of a String within another String.

How do you compare strings to ignore cases?

The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.

How do you ignore a case in python?

Approach No 1: Python String lower() Method This is the most popular approach to case-insensitive string comparisons in Python. The lower() method converts all the characters in a string to the lowercase, making it easier to compare two strings.

How do you change a case-sensitive string in Python?

Is the String replace function case sensitive? Yes, the replace function is case sensitive. That means, the word “this” has a different meaning to “This” or “THIS”. In the following example, a string is created with the different case letters, that is followed by using the Python replace string method.


2 Answers

Use a regular expression:

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

Of course, this will also match words containing camel, but it's not clear if you want that or not.

If you need exact matches you can use a custom MatchEvaluator.

public static class Evaluators {     public static string Wrap( Match m, string original, string format )     {         // doesn't match the entire string, otherwise it is a match         if (m.Length != original.Length)         {             // has a preceding letter or digit (i.e., not a real match).             if (m.Index != 0 && char.IsLetterOrDigit( original[m.Index - 1] ))             {                 return m.Value;             }             // has a trailing letter or digit (i.e., not a real match).             if (m.Index + m.Length != original.Length && char.IsLetterOrDigit( original[m.Index + m.Length] ))             {                 return m.Value;             }         }         // it is a match, apply the format         return string.Format( format, m.Value );     } }  

Used with the previous example to wrap the match in a span as:

var regex = new Regex( highlightedWord, RegexOptions.IgnoreCase ); foreach (var sentence in sentences) {     var evaluator = new MatchEvaluator( match => Evaluators.Wrap( match, sentence, "<span class='red'>{0}</span>" ) );     Console.WriteLine( regex.Replace( sentence, evaluator ) ); } 
like image 88
tvanfosson Avatar answered Sep 23 '22 15:09

tvanfosson


Add an extension method for string to do the trick:

Usage:

string yourString = "TEXTTOREPLACE"; yourString.Replace("texttoreplace", "Look, I Got Replaced!", StringComparison.OrdinalIgnoreCase); 

Code:

using System; using System.Collections.Generic; using System.IO;  public static class Extensions {            public static string Replace(this string source, string oldString, string newString, StringComparison comp)     {         int index = source.IndexOf(oldString, comp);          // Determine if we found a match         bool MatchFound = index >= 0;          if (MatchFound)         {             // Remove the old text             source = source.Remove(index, oldString.Length);              // Add the replacemenet text             source = source.Insert(index, newString);         }          // recurse for multiple instances of the name         if (source.IndexOf(oldString, comp) != -1)         {             source = Replace(source, oldString, newString, comp);         }          return source;     } } 
like image 21
Tom Beech Avatar answered Sep 21 '22 15:09

Tom Beech