Checking if a string is numeric...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • moo

    #1

    Checking if a string is numeric...

    Once again, an elegant way of doing it, and I'm sure there's a built-
    in library function for it. In Python, Strings have a isdigit()
    property that can be used to check if a string contains only digits,
    which while I haven't looked at the code I imagine checks if each
    character falls within the ascii character range of 0-9, etc.

    I could write my own, but I'm sure there's a similar, easy way of
    doing it in .Net. Anyone know? Double.isNaN() is out because I only
    want integers, and although I can use Convert.ToInt32 (), exception
    handling seems like a pretty crappy way of doing a conditional (I'm
    doing validation)..

  • Mark Rae [MVP]

    #2
    Re: Checking if a string is numeric...

    "moo" <ntv1534@gmail. comwrote in message
    news:1185489710 .319054.285360@ d55g2000hsg.goo glegroups.com.. .
    Anyone know?
    http://msdn2.microsoft.com/en-us/lib....tryparse.aspx


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

      #3
      Re: Checking if a string is numeric...

      moo wrote:
      Once again, an elegant way of doing it, and I'm sure there's a built-
      in library function for it. In Python, Strings have a isdigit()
      property that can be used to check if a string contains only digits,
      which while I haven't looked at the code I imagine checks if each
      character falls within the ascii character range of 0-9, etc.
      >
      I could write my own, but I'm sure there's a similar, easy way of
      doing it in .Net. Anyone know? Double.isNaN() is out because I only
      want integers, and although I can use Convert.ToInt32 (), exception
      handling seems like a pretty crappy way of doing a conditional (I'm
      doing validation)..
      int.TryParse
      a for loop and Char.IsDIgit
      Regex.IsMatch

      take your pick.

      Arne

      Comment

      • Peter Duniho

        #4
        Re: Checking if a string is numeric...

        On Thu, 26 Jul 2007 15:41:50 -0700, moo <ntv1534@gmail. comwrote:
        Once again, an elegant way of doing it, and I'm sure there's a built-
        in library function for it. In Python, Strings have a isdigit()
        property that can be used to check if a string contains only digits,
        which while I haven't looked at the code I imagine checks if each
        character falls within the ascii character range of 0-9, etc.
        I don't know Python, but I'd guess the isdigit() function is similar to
        Char.IsDigit(), which checks a specific character instance for being a
        digit. You can of course apply the test to a string of characters to test
        if they are all digits, but of course that's not a 100% perfect way of
        knowing whether a string is parseable as an integer, since there are
        strings that are all digits, but which are too long to be stored in an int.
        I could write my own, but I'm sure there's a similar, easy way of
        doing it in .Net. Anyone know? Double.isNaN() is out because I only
        want integers, and although I can use Convert.ToInt32 (), exception
        handling seems like a pretty crappy way of doing a conditional (I'm
        doing validation)..
        For every Parse, there's a TryParse.

        Okay, actually I'm not 100% sure about that. But for sure, there's an
        int.TryParse(). It returns a true/false value depending on whether the
        string could in fact be parsed as an integer.

        Pete

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Checking if a string is numeric...

          moo <ntv1534@gmail. comwrote:
          Once again, an elegant way of doing it, and I'm sure there's a built-
          in library function for it. In Python, Strings have a isdigit()
          property that can be used to check if a string contains only digits,
          which while I haven't looked at the code I imagine checks if each
          character falls within the ascii character range of 0-9, etc.
          >
          I could write my own, but I'm sure there's a similar, easy way of
          doing it in .Net. Anyone know? Double.isNaN() is out because I only
          want integers, and although I can use Convert.ToInt32 (), exception
          handling seems like a pretty crappy way of doing a conditional (I'm
          doing validation)..
          If you're using .NET 2.0, int.TryParse or long.TryParse are your
          friends. Note that they will return false if you give them a number
          which is too big to handle - for instance, if you pass in
          "99999999999999 9999999999999" that's too big for either of them despite
          it only containing digits. A simple method to encapsulate the logic (a
          foreach loop) would be the best solution if you wanted to handle that
          case.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • moo

            #6
            Re: Checking if a string is numeric...

            On Jul 26, 3:49 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
            moo wrote:
            Once again, an elegant way of doing it, and I'm sure there's a built-
            in library function for it. In Python, Strings have a isdigit()
            property that can be used to check if a string contains only digits,
            which while I haven't looked at the code I imagine checks if each
            character falls within the ascii character range of 0-9, etc.
            >
            I could write my own, but I'm sure there's a similar, easy way of
            doing it in .Net. Anyone know? Double.isNaN() is out because I only
            want integers, and although I can use Convert.ToInt32 (), exception
            handling seems like a pretty crappy way of doing a conditional (I'm
            doing validation)..
            >
            int.TryParse
            a for loop and Char.IsDIgit
            Regex.IsMatch
            >
            take your pick.
            >
            Arne
            Bah, TryParse sucks terribly since you have to pass in a reference!
            The for-loop is basically what I was talking about, and if that and
            the RegEx are the only options than I guess I'll just do
            those...anyone know which one has better performance?

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Checking if a string is numeric...

              moo <ntv1534@gmail. comwrote:
              Bah, TryParse sucks terribly since you have to pass in a reference!
              Well, you have to pass a parameter *by* reference (as an out parameter)
              which isn't quite the same thing.

              However, I'm interested to know in what way it "sucks terribly". It's
              not exactly hard to supply an out parameter.
              The for-loop is basically what I was talking about, and if that and
              the RegEx are the only options than I guess I'll just do
              those...anyone know which one has better performance?
              A for or foreach loop will perform better than a regex in this case.

              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              • Mark Rae [MVP]

                #8
                Re: Checking if a string is numeric...

                "moo" <ntv1534@gmail. comwrote in message
                news:1185491295 .580892.247960@ o61g2000hsh.goo glegroups.com.. .
                Bah, TryParse sucks terribly since you have to pass in a reference!
                What a strange statement! Care to elaborate...?


                --
                Mark Rae
                ASP.NET MVP


                Comment

                • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                  #9
                  Re: Checking if a string is numeric...

                  moo wrote:
                  On Jul 26, 3:49 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
                  >moo wrote:
                  >>Once again, an elegant way of doing it, and I'm sure there's a built-
                  >>in library function for it. In Python, Strings have a isdigit()
                  >>property that can be used to check if a string contains only digits,
                  >>which while I haven't looked at the code I imagine checks if each
                  >>character falls within the ascii character range of 0-9, etc.
                  >> I could write my own, but I'm sure there's a similar, easy way of
                  >>doing it in .Net. Anyone know? Double.isNaN() is out because I only
                  >>want integers, and although I can use Convert.ToInt32 (), exception
                  >>handling seems like a pretty crappy way of doing a conditional (I'm
                  >>doing validation)..
                  >int.TryParse
                  >a for loop and Char.IsDIgit
                  >Regex.IsMatc h
                  >>
                  >take your pick.
                  >
                  Bah, TryParse sucks terribly since you have to pass in a reference!
                  That is more or less given by its functionality.
                  The for-loop is basically what I was talking about, and if that and
                  the RegEx are the only options than I guess I'll just do
                  those...anyone know which one has better performance?
                  The for loop is much faster. Not as in 10% faster but
                  as in x10 faster.

                  Arne

                  Comment

                  • =?UTF-8?B?QXJuZSBWYWpow7hq?=

                    #10
                    Re: Checking if a string is numeric...

                    Peter Duniho wrote:
                    On Thu, 26 Jul 2007 15:41:50 -0700, moo <ntv1534@gmail. comwrote:
                    >Once again, an elegant way of doing it, and I'm sure there's a built-
                    >in library function for it. In Python, Strings have a isdigit()
                    >property that can be used to check if a string contains only digits,
                    >which while I haven't looked at the code I imagine checks if each
                    >character falls within the ascii character range of 0-9, etc.
                    >
                    I don't know Python, but I'd guess the isdigit() function is similar to
                    Char.IsDigit(), which checks a specific character instance for being a
                    digit. You can of course apply the test to a string of characters to
                    test if they are all digits, but of course that's not a 100% perfect way
                    of knowing whether a string is parseable as an integer, since there are
                    strings that are all digits, but which are too long to be stored in an int.
                    Python isdigit is for string not for char.

                    In fact Python does not have a char type.

                    Arne

                    Comment

                    Working...