Check if string is numeric in one line of code

28,794

Solution 1

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..
}

Solution 2

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

Solution 3

How about some easy LinQ?

if (str.Take(4).All(char.IsDigit) { ... }
Share:
28,794
TheIronCheek
Author by

TheIronCheek

Updated on September 29, 2020

Comments

  • TheIronCheek
    TheIronCheek over 3 years

    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?

  • Jonesopolis
    Jonesopolis almost 8 years
    problem is this doesn't !00% verify it's an int
  • Scott
    Scott almost 8 years
    @Jonesopolis True, it's a workaround for sure. The extension method approach is better, but if OP is just after a basic "string only contains numbers" method, it should work.
  • AgentFire
    AgentFire almost 8 years
    Doesn't handle long numeric numbers, nor does it handle comma/period delimiters.
  • Zein Makki
    Zein Makki almost 8 years
    This answers the question, but using @this as a parameter name is a very, very bad choice. Not to mention using it as an extension method parameter. Personally i find using keywords as parameter and variable names as something to be avoided.
  • Scott
    Scott almost 8 years
    @AgentFire Neither does int.TryParse. I'll update to use float.TryParse for the extension method. This problem could be solved in a lot of ways (including regex), the "best" solution just depends on what the OP needs.
  • smoksnes
    smoksnes almost 8 years
    @user3185569 - You might be right regarding the parameter name. I changed it to input. Personally I find @this very logical in this (pun not intended) particular case just because it shows what your extension is extending. But that's just me.
  • smoksnes
    smoksnes almost 8 years
    @AgentFire - Yes, in that case you might want to use Int64.TryParse, but I understood the OP as int.TryParse could be used, but not in a single line.
  • TheIronCheek
    TheIronCheek almost 8 years
    You guys don't have to make it account for anything more complicated than a positive integer. I'll update the question to be more specific about this.
  • John Demetriou
    John Demetriou over 7 years
    What about negatives and decimals?
  • M. Fawad Surosh
    M. Fawad Surosh over 6 years
    "".All(char.IsDigit); returns "TRUE"
  • Scott
    Scott over 6 years
    @M.FawadSurosh Personally I would handle that as a different validation case. For example, 1) the string should have a complete value (!string.IsNullOrEmpty), and 2) string is numeric (my solution)
  • M. Fawad Surosh
    M. Fawad Surosh over 6 years
    I have already done it that way but I would love it if I could handle all in one condition.
  • Scott
    Scott over 6 years
    @M.FawadSurosh In that case, the IsNumeric extension method from my answer might work for you. It simply tries to parse the string as a float, which will handle decimals, negatives, numbers like 1e3, etc.
  • Roni Tovi
    Roni Tovi over 6 years
    return int.TryParse(input, out int number); saves 1 line.
  • Momoro
    Momoro almost 3 years
    This is great, since I'm only iterating through one character at a time in a string, and I need to check if that character is a number. Thanks!