6

How can I check if my input is a particular kind of string. So no numeric, no "/",...

skiphoppy
  • 97,646
  • 72
  • 174
  • 218
senzacionale
  • 20,448
  • 67
  • 204
  • 316

5 Answers5

15

Well, to check that an input is actually an object of type System.String, you can simply do:

bool IsString(object value)
{
    return value is string;
}

To check that a string contains only letters, you could do something like this:

bool IsAllAlphabetic(string value)
{
    foreach (char c in value)
    {
        if (!char.IsLetter(c))
            return false;
    }

    return true;
}

If you wanted to combine these, you could do so:

bool IsAlphabeticString(object value)
{
    string str = value as string;
    return str != null && IsAllAlphabetic(str);
}
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
10

If you mean "is the string completely letters", you could do:

string myString = "RandomStringOfLetters";
bool allLetters = myString.All( c => Char.IsLetter(c) );

This is based on LINQ and the Char.IsLetter method.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
6

It's not entirely clear what you want, but you can probably do it with a regular expression. For example to check that your string contains only letters in a-z or A-Z you can do this:

string s = "dasglakgsklg";
if (Regex.IsMatch(s, "^[a-z]+$", RegexOptions.IgnoreCase))
{
    Console.WriteLine("Only letters in a-z.");
}
else
{
    // Not only letters in a-z.
}

If you also want to allow spaces, underscores, or other characters simply add them between the square brackets in the regular expression. Note that some characters have a special meaning inside regular expression character classes and need to be escaped with a backslash.

You can also use \p{L} instead of [a-z] to match any Unicode character that is considered to be a letter, including letters in foreign alphabets.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2
using System.Linq;
...

bool onlyAlphas = s.All(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Don't get me wrong: regex has its uses. But for a simple, single-pass algorithm like this, it's overkill. I like Reed's answer more, though. Didn't know about `Char.IsLetter`. – StriplingWarrior Jul 20 '10 at 18:55
  • @Luiscencio: How am I being brutal? – StriplingWarrior Jul 20 '10 at 18:59
  • I think Luiscencio meant that you used brute force to solve it. I would have written a for loop. You solution using LINQ is far less brute force than that. – aaaa bbbb Jul 20 '10 at 19:03
  • Huh. I always thought of brute force as having to do with performance. Brute force would have been throwing all valid characters into an array and doing a doubly-nested loop to make sure that all the characters in that string are one of those characters. Not having known about Char.IsLetter, the approach I took seemed pretty elegant to me. To each his own, though. – StriplingWarrior Jul 20 '10 at 19:36
1

Something like this (have not tested) may fit your (vague) requirement.

if (input is string)
{
    // test for legal characters?
    string pattern = "^[A-Za-z]+$";
    if (Regex.IsMatch(input, pattern))
    {
         // legal string? do something 
    }

    // or
    if (input.Any(c => !char.IsLetter(c)))
    {
         // NOT legal string 
    }
}
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246