Skip to content

Instantly share code, notes, and snippets.

@motowilliams
Created October 23, 2011 18:47
Show Gist options
  • Save motowilliams/1307701 to your computer and use it in GitHub Desktop.
Save motowilliams/1307701 to your computer and use it in GitHub Desktop.
Checking-for-special-characters-using-LINQ
public static class StringExtensions
{
public static bool HasInvalidCharacters(this string value)
{
//Ascii range between 32 and 127
IEnumerable<int> lowRange = Enumerable.Range(0, 32);
IEnumerable<int> highRange = Enumerable.Range(128, 128);
IEnumerable<int> enumerable = value.ToCharArray().Select(x => Convert.ToInt32(((int)x).ToString()));
return lowRange.Intersect(enumerable).Count() != 0 || highRange.Intersect(enumerable).Count() != 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment