I want the first two and the last 4 characters to be numbers and the rest to remain as strings. If these conditions are not met, the license plate number should be obtained again.I couldn't go further than the code below.
static string PlakaAl(string plaka)
{
int sayi;
while (true)
{
Console.Write(plaka);
string giris = Console.ReadLine();
if (giris.Length == 8 && int.TryParse(giris.Substring(0, 2), out sayi) == true && int.TryParse(giris.Substring(4, 4), out sayi == true)
{
return plaka;
}
else
{
Console.WriteLine("Bu şekilde plaka girişi yapamazsınız. Tekrar deneyin.");
}
}
This seems like it'd be a perfect opportunity to use a regular expression.
You can use this to define a pattern for the expected format of these license plates. This pattern ought to work for you based on your question:
Here's an explanation of what each part of this pattern will match:
^- the start of the string\d{2}- two digits[A-Z]{2}- two uppercase letters\d{4}- four digits$- the end of the stringNote: I wasn't clear what you meant by "the rest to remain as strings". If you want to allow more than alphabetic characters, you'd just need to update the
[A-Z]character group to include any other characters that are acceptable.You can also see a visualization of this pattern here to help understand what it's doing.
You'd use it like this: