Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is an alternative to string.Replace that is case-insensitive in C#?
The standard string.Replace() method in C# is case-sensitive by default. When you need case-insensitive string replacement, there are several alternative approaches you can use. The most common methods include using regular expressions, the StringComparison parameter (available in .NET 5+), or creating custom replacement methods.
Using Regular Expressions for Case-Insensitive Replace
Regular expressions provide the most flexible approach for case-insensitive string replacement using RegexOptions.IgnoreCase −
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
string str = "Cricket Team";
Console.WriteLine("Original String: " + str);
string result = CaseInsensitiveReplace(str, "TEAM", "Squad");
Console.WriteLine("After Replace: " + result);
result = CaseInsensitiveReplace(str, "cricket", "Football");
Console.WriteLine("After Replace: " + result);
}
static string CaseInsensitiveReplace(string originalString, string oldValue, string newValue) {
Regex regex = new Regex(Regex.Escape(oldValue), RegexOptions.IgnoreCase);
return regex.Replace(originalString, newValue);
}
}
The output of the above code is −
Original String: Cricket Team After Replace: Cricket Squad After Replace: Football Team
Using StringComparison (Available in .NET 5+)
Starting with .NET 5, the string.Replace() method supports a StringComparison parameter for case-insensitive operations −
using System;
class Program {
static void Main(string[] args) {
string str = "Hello World";
Console.WriteLine("Original: " + str);
// Case-insensitive replace using StringComparison
string result = str.Replace("HELLO", "Hi", StringComparison.OrdinalIgnoreCase);
Console.WriteLine("After Replace: " + result);
result = str.Replace("world", "Universe", StringComparison.OrdinalIgnoreCase);
Console.WriteLine("After Replace: " + result);
}
}
The output of the above code is −
Original: Hello World After Replace: Hi World After Replace: Hello Universe
Custom Case-Insensitive Replace Method
For compatibility with older .NET versions, you can create a custom method using IndexOf() with StringComparison −
using System;
class Program {
static void Main(string[] args) {
string str = "The Cat in the Hat";
Console.WriteLine("Original: " + str);
string result = ReplaceIgnoreCase(str, "cat", "Dog");
Console.WriteLine("After Replace: " + result);
result = ReplaceIgnoreCase(str, "THE", "A");
Console.WriteLine("After Replace: " + result);
}
static string ReplaceIgnoreCase(string source, string oldValue, string newValue) {
int index = source.IndexOf(oldValue, StringComparison.OrdinalIgnoreCase);
while (index >= 0) {
source = source.Remove(index, oldValue.Length);
source = source.Insert(index, newValue);
index = source.IndexOf(oldValue, index + newValue.Length, StringComparison.OrdinalIgnoreCase);
}
return source;
}
}
The output of the above code is −
Original: The Cat in the Hat After Replace: A Dog in A Hat After Replace: A Cat in A Hat
Comparison of Methods
| Method | .NET Version | Performance | Best For |
|---|---|---|---|
| Regular Expressions | All versions | Good for complex patterns | Pattern matching, special characters |
| StringComparison Parameter | .NET 5+ | Best performance | Simple string replacement |
| Custom IndexOf Method | All versions | Good performance | Legacy .NET Framework projects |
Conclusion
For case-insensitive string replacement in C#, use the StringComparison.OrdinalIgnoreCase parameter with Replace() in .NET 5+, or regular expressions with RegexOptions.IgnoreCase for older versions. Choose the method based on your .NET version and performance requirements.
