Skip to content

Instantly share code, notes, and snippets.

@jeremykdev
Created July 11, 2013 20:28
Show Gist options
  • Save jeremykdev/5978945 to your computer and use it in GitHub Desktop.
Save jeremykdev/5978945 to your computer and use it in GitHub Desktop.
Collection of extension methods for working with strings in .NET
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace net.datacowboy
{
public static class StringExtensionMethods
{
/// <summary>
/// Remove non-digit characters from a string
/// </summary>
public static string StripNonDigitCharacters(this string input)
{
Regex re = new Regex(@"\D");
return re.Replace(input, String.Empty);
}
/// <summary>
/// Test if string matches a regular expression pattern
/// </summary>
public static bool IsRegularExpressionMatch(this string input, string regularExpressionPattern)
{
return input.IsRegularExpressionMatch(new Regex(regularExpressionPattern));
}
/// <summary>
/// Test if string matches a regular expression
/// </summary>
public static bool IsRegularExpressionMatch(this string input, Regex regExp)
{
return regExp.IsMatch(input);
}
/// <summary>
/// Test if string length within given range
/// </summary>
public static bool IsLengthWithinRange(this string input, int min, int max)
{
return (input.Length >= min && input.Length <= max);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment