C#: Case-Insensitive String Contains Best Practices

Oct 18, 2024  • C#

When working with strings in C#, handling case-insensitive searches is often necessary. The String.Contains method in C# is case-sensitive by default, but there are various ways to perform case-insensitive checks. Based on the official Microsoft documentation, here are some best practices and methods you can employ:

1. Leverage String.Contains with StringComparison

Starting with .NET Core 2.1 and continuing through .NET 5.0 and beyond, you can directly use the String.Contains method with a StringComparison argument, making case-insensitive comparisons cleaner and more intuitive.

string title = "Hello World";
bool contains = title.Contains("hello", StringComparison.OrdinalIgnoreCase);

This method provides a more modern and straightforward syntax, and if you’re using a version of .NET that supports it, this should be your go-to choice.

2. Use IndexOf with StringComparison.OrdinalIgnoreCase

For efficient, case-insensitive string matching, using IndexOf with StringComparison.OrdinalIgnoreCase is one of the most popular approaches. This method checks if a substring exists within the main string while ignoring case differences.

string title = "Hello World";
bool contains = title.IndexOf("hello", StringComparison.OrdinalIgnoreCase) >= 0;

This approach is lightweight, easy to implement, and performs well for most use cases where performance is a concern.

3. Handle Culture-Sensitive Comparisons with CompareInfo.IndexOf

For applications that require culture-aware string comparisons, especially in globalized environments, use CompareInfo.IndexOf to respect language-specific casing rules. For example, some languages, like Turkish, treat casing differently, and using the right comparison method is essential.

var culture = new CultureInfo("tr-TR");
bool contains = culture.CompareInfo.IndexOf(title, "hello", CompareOptions.IgnoreCase) >= 0;

This method ensures that your string comparisons are culturally accurate and sensitive, making it crucial for internationalized applications.

4. Avoid Regular Expressions for Simple Searches

Although regular expressions (Regex) offer case-insensitive string matching, they are generally overkill for basic substring searches. Regular expressions can introduce performance overhead and add unnecessary complexity unless you’re dealing with more sophisticated pattern matching.

bool contains = Regex.IsMatch("Hello World", Regex.Escape("hello"), RegexOptions.IgnoreCase);

While powerful, regex is typically unnecessary for most simple string search tasks.

Summary of Recommendations

  • For most general cases: Use IndexOf with StringComparison.OrdinalIgnoreCase for both performance and simplicity.
  • For modern .NET versions: If you’re working with .NET Core 2.1 or later, the Contains method with StringComparison is the cleanest and most direct solution.
  • For culturally-sensitive applications: Utilize CompareInfo.IndexOf to ensure proper handling of language-specific casing rules.
  • Avoid complexity: Don’t use regular expressions unless needed, as they can slow down your application for simple searches.

Conclusion

Choosing the right method for case-insensitive string comparisons depends on the requirements of your application. Whether you’re optimizing for performance, simplicity, or cultural accuracy, C# provides flexible options for achieving your goal. Make sure to choose the approach that fits your project’s needs and framework version.