Samoop
Samoop

Reputation: 51

Identify string contents, and verify

I'm still fairly new to C#,

I'm making a 'mini-username checker' if you will

so far, the username is 2 numbers, followed by a name,

example

13Samuel

which can also be typed as

13samuel

What I am trying to do is detect if the first two characters makes a number between 0 and 99 then detect if the third character is a letter (a-z) lowercase or caps.

Thank you for reading,

Samuel

Upvotes: 2

Views: 240

Answers (5)

Mbjahnoon
Mbjahnoon

Reputation: 31

There you go:

using System.Text.RegularExpressions;
    static bool Answer(string stringToTest)
    {
        var classifier = @"^\d\d[a-zA-Z]";
        Regex regex = new Regex(classifier);
        return regex.Match(stringToTest).Success;
    }

So remember to use regex and you could use https://regex101.com/ to test your classifiers

Upvotes: 0

alphuman
alphuman

Reputation: 31

the following methods returns whether your string is valid:

public static bool validateString(string stringToValidate) {
    Regex rgx = new Regex(@"^\d{1,2}[a-zA-Z]+$");
        return rgx.Match(stringToValidate).Success;
    }

explanation:

^ - marks the beginning of your string

\d - matches all digits from 0-9

{1,2} - digits may occur 1-2 times

[a-zA-Z] - matches all literals from a-z and from A-Z

+ - matches the literals 1 or multiple times

you can check the documentation for further information about Regular Expresssions in C#: https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.7.2

Upvotes: 0

Prasad Telkikar
Prasad Telkikar

Reputation: 16079

If you want to check first two letters are in between 0 to 99 and followed by character then you can try followings

    string name = "13Samuel";

    if(name.Length > 2)
    {
        int number;
        if(Int32.TryParse(name.Substring(0,2), out number)){
            Console.WriteLine(number > 0 && number < 99 & Char.IsLetter(name[2]) ? "Success" : "Failed");
        }

    }

POC: .Net Fiddler

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460288

There's certainly a regex approach but with string methods and LINQ it's imo easier to read:

string name = "13Samuel";
bool valid = name.Length > 2 && name.Remove(2).All(char.IsDigit) && char.IsLetter(name[2]);

or maybe you don't want to allow all letters but just a-z and A-Z:

// store this in a field so that it doesn't always need to be generated 
char[] allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".SelectMany(c => new[]{c, char.ToLower(c)}).ToArray(); 

bool valid = name.Length > 2 && name.Remove(2).All(char.IsDigit) && allowed.Contains(name[2]);

Upvotes: 3

J.Doe
J.Doe

Reputation: 308

I was editing a previous post but it was removed.

You can use regex to match strings to formats

For example :

Regex regex = new Regex(@"[\d]{2}[a-zA-Z]+");
    Match match = regex.Match(username);
    if (match.Success)
    {
        //it is valid
    }

for this regex "[\d]{2}" means 2 digits and [a-zA-Z]+ is any number of letters from the alphabet.

You can check the documentation for more info on how to use regex in C#: https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=netframework-4.7.2

Upvotes: 1

Related Questions