How to see if a string contains another…
Ok, so you already know the answer, right? That’s why the String class has the Contains method. And it will work until you need to explicitly need to use a different StringComparison option than the one that is used by default.
If like me, you know that Reflector is your friend, then you can just open it and see how the Contains method is implemented. Basically, what you need is a new extension method that looks like this:
public static class StringExtensions {
public static Boolean Contains(this String str, String value, StringComparison options) {
return str.IndexOf(value, options) >= 0;
}
}
Which means that you’ll be able to use it like this:
var name = “luis”;
var anotherName = “LUIS MIGUEL”;
var exists = anotherName.Contains(“luis”, StringComparison.CurrentCultureIgnoreCase );
Bottom line: you simply cannot live without Reflector and C# 3.0!