How can I check if my input is a particular kind of string. So no numeric, no "/",...
-
Do you by any chance mean alphabetic (letters only) instead of string? – BoltClock Jul 20 '10 at 18:43
-
1Do you mean, "How can I check if a string contains only characters?" – Justin Niessner Jul 20 '10 at 18:44
-
1Do you mean a string consisting only of alpha-numeric characters? Can you define which characters exactly? – Pekka Jul 20 '10 at 18:45
-
1Are you wanting validation on a UI control, or on some backend process? If a UI, are you talking about WinForms, WebForms, Silverlight, WPF...? – Pedro Jul 20 '10 at 18:46
-
if your input control is a textbox it will always be a string =) – Luiscencio Jul 20 '10 at 18:46
-
There was little to no effort put in this question, or effort to rephrase it when others asked. – May 05 '16 at 02:34
5 Answers
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);
}

- 125,917
- 54
- 300
- 447
-
1Good example of both, being that the title and question don't exactly match. – corsiKa Jul 20 '10 at 18:49
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.

- 554,122
- 78
- 1,158
- 1,373
-
@ahsteele: It actually System.Globalization.CharUnicodeInfo to handle this for non latin characters. – Reed Copsey Jul 20 '10 at 18:59
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.

- 811,555
- 193
- 1,581
- 1,452
using System.Linq;
...
bool onlyAlphas = s.All(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));

- 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
-
-
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
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
}
}

- 123,721
- 27
- 225
- 246