TheIronCheek
TheIronCheek

Reputation: 1149

Check if string is numeric in one line of code

I'm working with a DNN form-building module that allows for some server-side code to be run based on a condition. For my particular scenario, I need my block of code to run if the first 4 characters of a certain form text are numeric.

The space to type the condition, though, is only one line and I believe gets injected into an if statement somewhere behind the scenes so I don't have the ability to write a mult-line conditional.

If I have a form field called MyField, I might create a simple conditional like this:

[MyField] == "some value"

Then somewhere behind the scenes it gets translated to something like if("some value" == "some value") {

I know that int.TryParse() can be used to determine whether or not a string is numeric but every implementation I've seen requires two lines of code, the first to declare a variable to contain the converted integer and the second to run the actual function.

Is there a way to check to see if the first 4 characters of a string are numeric in just one line that can exist inside an if statement?

Upvotes: 3

Views: 37602

Answers (3)

Scott
Scott

Reputation: 5379

In response to this:

Is there a way to check to see if the first 4 characters of a string are numeric in just one line that can exist inside an if statement?

You guys don't have to make it account for anything more complicated than a positive integer.

new Regex(@"^\d{4}").IsMatch("3466") // true
new Regex(@"^\d{4}").IsMatch("6")    // false
new Regex(@"^\d{4}").IsMatch("68ab") // false
new Regex(@"^\d{4}").IsMatch("1111abcdefg") // true

// in an if:
if (new Regex(@"^\d{4}").IsMatch("3466"))
{
    // Do something
}

Old answer:

If you can't use TryParse, you could probably get away with using LINQ:

"12345".All(char.IsDigit); // true
"abcde".All(char.IsDigit); // false
"abc123".All(char.IsDigit); // false

If you can, here's an IsNumeric extension method, with usage:

public static class NumberExtensions
{
    // <= C#6
    public static bool IsNumeric(this string str)
    {
        float f;
        return float.TryParse(str, out f);
    }

    // C# 7+
    public static bool IsNumeric(this string str) => float.TryParse(str, out var _);
}

// ... elsewhere

"123".IsNumeric();     // true
"abc".IsNumeric();     // false, etc
"-1.7e5".IsNumeric();  // true

Upvotes: 4

AgentFire
AgentFire

Reputation: 9800

How about some easy LinQ?

if (str.Take(4).All(char.IsDigit) { ... }

Upvotes: 3

smoksnes
smoksnes

Reputation: 10871

Wrap it in an extension method.

public static class StringExtensions
{
    public static bool IsNumeric(this string input)
    {
        int number;
        return int.TryParse(input, out number);
    }
}

And use it like

if("1234".IsNumeric())
{
    // Do stuff..
}

UPDATE since question changed:

public static class StringExtensions
{
    public static bool FirstFourAreNumeric(this string input)
    {
        int number;
        if(string.IsNullOrEmpty(input) || input.Length < 4)
        {
            throw new Exception("Not 4 chars long");
        }

        return int.TryParse(input.Substring(4), out number);
    }
}

And use it like

if("1234abc".FirstFourAreNumeric())
{
    // Do stuff..
}

Upvotes: 11

Related Questions