C# - String.Contains with StringComparison

By , 9/21/2015
(0 ratings)
This snippets returns a value indicating whether a specified substring occurs within this string, the review uses the given StringComparison.

This method is implemented as an extension for System.String. That's why you can use it like the Contains-method without a StringComparison.
/// <summary>
/// Returns a value indicating whether a specified substring occurs within this string, the review uses the given <see cref="System.StringComparison"/>.
/// </summary>
/// <param name="source">The source string to search in.</param>
/// <param name="value">The string to seek.</param>
/// <param name="comparisonType">The rule for the search.</param>
/// <returns><c>true</c> if <paramref name="value"/> occurs in <paramref name="source"/> in accordance with <paramref name="comparisonType"/>;otherwise <c>false</c></returns>
public static bool Contains(this string source, string value, StringComparison comparisonType)
{
    return source.IndexOf(value, comparisonType) >= 0;
}

Comments

 

Log in, to comment!