开发者

Defining The Range For Alphabets in C#

I'm having trouble creating a marksheet program in C# 2.0, here is the code of it

Consol开发者_高级运维e.WriteLine("Prislogix Public School");
Console.WriteLine("\n\nMarksheet\n\n");
Console.WriteLine("Enter Student Name : ");
string name = Console.ReadLine();
Console.WriteLine("\nEnter Class : ");
string cls = Console.ReadLine();
Console.WriteLine("\nEnter Roll Number : ");
int roll = int.Parse(Console.ReadLine());

This is the basic write and read task. What I want to create is a condition for name. For example, if a user enters 123 in the name field, it takes the name as 123.

I don't want it to allow any numbers in the name field. How can this be done? Do I have to define the range for ASCII Codes for alphabets? I think a do..while loop will be used but what how should I define range between alphabets (A To Z or a to z).


You could do it simply like this:

if(!Regex.Matches(name, "^[a-zA-Z]+$"))
    // name is invalid


Match against a regular expression:

if (!Regex.Match(name, "^([A-Za-z ]+)$").Success)
{
  // Error message here.
}

This will also allow spaces in people's names, which is probably something you want.

More about regular expressions in C# here: http://tim.oreilly.com/pub/a/oreilly/windows/news/csharp_0101.html


Match match = Regex.Match(name, @"[A-Za-z]+", RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
...
}


You can use RegEx class and see if it matches your input.


You can use a regular expression. If they input invalid entry, reject the entry, display a message and request the input again. You could do all of that in 5 lines with a single while loop for each Console.ReadLine().

also:

int roll = int.Parse(Console.ReadLine()); 

int.Parse will cause an exception if the string contains any non-numerical characters.


The most foolproof way, and probably the easiest besides, would be to run what the user has entered through a regex:

...
var validName = false;
 while (!validName)
 {
    if(!Regex.Match(name, "^[A-Za-z ]+$")
      Console.WriteLine("Invalid name; try again");
    else
      validName = true;
 }
...


If the test is, that the string should not contain any digit (all other characters are allowed), you can use a regular expression:

var regExContainsDigit = new Regex(@"\d");

if (regExContainsDigit.IsMatch(name)) {
   // contains at least 1 digit
}

It becomes more complicated if a name is only allowed letters. Then you should also think about spaces, dots, letters with diacrits (ë é), etc.


string sPattern = "^[A-Za-z]+$"
bool isValid = false;
while(!isValid)
{
   Console.WriteLine("Enter Student Name : ");
   string name = Console.ReadLine();
   isValid = System.Text.RegularExpressions.Regex.Match(name, sPattern);
}

basically check the name value against regex until they get it right


You could do this with a simple loop

string name = String.empty
do
{
  Console.WriteLine("Enter Student Name : ");
  name = Console.ReadLine()
}
while(!Regex.Match(name, "^([A-Za-z ]+)$").Success);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜