Friday, November 4, 2016

String extension for Contains, with StringComparison

So, many of the built in string methods have the possibility to set the StringComparison in code, to for example OrdinalIgnoreCase wich is my favorite for most back-end processing.
Sad thing is, the Contains does not have it. So here is a small extension that fixes it with the help of IndexOf method

Some source-code (C#) to copy into your utilities class.

public static bool Contains(this string s, string sub, StringComparison comparison)
{
    return s.IndexOf(sub, comparison) != -1;
}

Hope this helps someone out there :)

Back to extensions list

No comments:

Post a Comment