It's quite simple, use IndexOf()
that has a parameter indicating that you want to bypass ignore the sensitivity of the box . Of course, it will return to the position where it is what you want to know if it exists, but then just check if the number is positive, since we know that a negative number means non-existence.
Someone makes an extension method available to type String
whenever you need it. Some people do not like it. If you prefer to do it on hand, just use what's inside the method. You can do an extension method that already uses the fixed comparison option and do not parametrize it.
using System;
public class Program {
public static void Main() {
var mainStr = "Joaquim Pedro Soares";
Console.WriteLine(mainStr.Contains("JOA", StringComparison.OrdinalIgnoreCase));
Console.WriteLine(mainStr.Contains("Quim", StringComparison.OrdinalIgnoreCase));
Console.WriteLine(mainStr.Contains("PEDRO", StringComparison.OrdinalIgnoreCase));
Console.WriteLine(mainStr.Contains("PeDro", StringComparison.OrdinalIgnoreCase));
}
}
namespace System {
public static class StringExt {
public static bool Contains(this string source, string search, StringComparison comparison) {
return source.IndexOf(search, comparison) >= 0;
}
}
}
See running on .NET Fiddle . And at Coding Ground . Also I put it in GitHub for future reference .
Some optimizations and improvements can be made, such as checking whether the parameters are null or empty.