Udostępnij za pośrednictwem


String extension methods rather than using RegEx

In a recent project I was using regular expressions a lot for validating string expressions such that statements like the following were true:

  • Must be all characters
  • Must be all alphanumeric’s and be of length 6
  • Must be all numeric digits (e.g. “3648”)
  • Must be all upper case characters

However in reflection the char data type checks, such as IsDigit(), along with extension methods offer an alternative implementation. What I wanted to write was validations such as:

mystring.IsAllLetterOrDigit();
mystring.IsAllLetter(3);
mystring.IsAllLower();

The implementation to support such expressions is:

/// <summary>
/// Determines if all characters are letters.
/// </summary>
public static bool IsAllLetter(this string value, [Optional] int length)
{
    return IsAllValidation(value, char.IsLetter, length);
}

/// <summary>
/// Determines if all characters are digits.
/// </summary>
public static bool IsAllDigit(this string value, [Optional] int length)
{
    return IsAllValidation(value, char.IsDigit, length);
}

/// <summary>
/// Determines if all characters are letters or digits.
/// </summary>
public static bool IsAllLetterOrDigit(this string value, [Optional] int length)
{
    return IsAllValidation(value, char.IsLetterOrDigit, length);
}

/// <summary>
/// Determines if all characters are numbers.
/// </summary>
public static bool IsAllNumber(this string value, [Optional] int length)
{
    return IsAllValidation(value, char.IsNumber, length);
}

/// <summary>
/// Determines if all characters are lower characters.
/// </summary>
public static bool IsAllLower(this string value, [Optional] int length)
{
    return IsAllValidation(value, char.IsLower, length);
}

/// <summary>
/// Determines if all characters are upper characters.
/// </summary>
public static bool IsAllUpper(this string value, [Optional] int length)
{
    return IsAllValidation(value, char.IsUpper, length);
}

/// <summary>
/// Determines if all characters in a string satisfy the character check.
/// </summary>
private static bool IsAllValidation(string value, Func<char, bool> charFunc, int length)
{
    // Check have values
    if (length < 0) throw new ArgumentOutOfRangeException("Length must be positive.");

    // Check have more than whitespace
    if (string.IsNullOrWhiteSpace(value)) return false;

    // Check all string characters satisfy condition
    if (!value.All<char>(charFunc)) return false;
    
    // If neccessary check strign length
    if (length > 0 && value.Length != length) return false;

    return true;
}

This provides a variety of checks all based on the char IsXXX() methods.

The IsAllValidation() is the actual implementation for all extension methods. As a parameter it takes the char based method for which each char in the string is to be checked. This works based on the fact that the String Type supports  IEnumerable of Type Char. We can then use the LINQ All<char>() method to ensure that each char in the string satisfies the required condition.

The length check is an optional check specified using the Optional Attribute on the length parameter. If a value greater than zero is specified then the string is also validated to ensure it has the specified length.

As you can see, with a small amount of code one can very easily extend char methods to support checking each char in a string.

Enjoy!