Skip to content

Instantly share code, notes, and snippets.

@ertanturan
Last active December 2, 2022 21:43
Show Gist options
  • Save ertanturan/f14f4d31e75e51436bb6a9e023d542e9 to your computer and use it in GitHub Desktop.
Save ertanturan/f14f4d31e75e51436bb6a9e023d542e9 to your computer and use it in GitHub Desktop.
case insensitive contains and equals in the least garbage-making way possible
public static class StringExtensions
{
/// <summary>
/// Check if a string contains another case insensitively.
/// </summary>
/// <param name="text">string to search in</param>
/// <param name="value">string to search for</param>
/// <param name="stringComparison">comparison option enum</param>
/// <returns></returns>
public static bool CaseInsensitiveContains(this string text, string value,
StringComparison stringComparison = StringComparison.CurrentCultureIgnoreCase) =>
text.IndexOf(value, stringComparison) >= 0;
/// <summary>
/// Compares strings creating the least amount of garbage possible
/// </summary>
/// <param name="firstString">first string to compare</param>
/// <param name="secondString">second string to compare</param>
/// <returns></returns>
public static bool CaseInsensitiveEquals (string firstString, string secondString) => string.Equals(firstString,secondString,StringComparison.OrdinalIgnoreCase);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment