I am using C# and I want to convert a string to int to verify name. For example ** or 12 is not a name. I just want to convert the string into ASCII values and then will verify the name. How do I do that?
-
3SO needs to reject any question that contains the string "wanna"Michael Mrozek– Michael Mrozek2010-06-25 05:04:46 +00:00Commented Jun 25, 2010 at 5:04
-
+1 @Michael. I still have no clue what the real question is, just gave a stab at it based on the title itself.drharris– drharris2010-06-25 05:07:05 +00:00Commented Jun 25, 2010 at 5:07
-
I fear you are doing things too complicated. To verify a name doesn't contain numbers you could just use the string.contains() method for exampleMohnkuchenzentrale– Mohnkuchenzentrale2010-06-25 05:08:13 +00:00Commented Jun 25, 2010 at 5:08
-
Another +1 from me. I had a stab based on the content of the message instead. :)EMP– EMP2010-06-25 05:08:44 +00:00Commented Jun 25, 2010 at 5:08
-
+1 from me, the question is legit, the title is misleading.Cosmin Prund– Cosmin Prund2010-06-25 05:29:04 +00:00Commented Jun 25, 2010 at 5:29
4 Answers
From what I understand, you want to verify that a given string represents a valid name? I'd say you should probably provide more details as to what constitutes a valid name to you, but I can take a stab at it. You could always iterate over all the characters in the string, making sure they're letters or white space:
public bool IsValidName(string theString)
{
for (int i = 0; i < theString.Length - 1; i++)
{
if (!char.IsLetter(theString[i]) && !char.IsWhiteSpace(theString[i]))
{
return false;
}
}
return true;
}
Of course names can have other legitimate characters, such as apostrophe ' so you'd have to customize this a bit, but it's a starting point from what I understand your question truly is. (Evidently, not all white space characters would qualify as acceptable either.)