C# seems to lack function that PHP has

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

    C# seems to lack function that PHP has

    I want to check if a string "123" is a number. but C# seems to lack a
    method tat does tat. the closest is char.isNumber where you can only
    check one character at a time anyway.

    Anyone knows a way around this? Or is there a method to do this
    already?

    Thanks
  • Jon Skeet [C# MVP]

    #2
    Re: C# seems to lack function that PHP has

    harrylmh <minghong@hotpo p.com> wrote:[color=blue]
    > I want to check if a string "123" is a number. but C# seems to lack a
    > method tat does tat. the closest is char.isNumber where you can only
    > check one character at a time anyway.
    >
    > Anyone knows a way around this? Or is there a method to do this
    > already?[/color]

    Well, you can try to parse it using Int32.Parse or Convert.ToInt32 , and
    catch the exception. Or you can use Double.TryParse with appropriate
    options. Or you can use a simple home-grown method which checks for
    every character being between 0 and 9 (to get rid of "simple"
    violations) and then use either of the other options to check more
    complicated things. I've found this hybrid approach to be the fastest
    in simple benchmarking scenarios.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Benny Mathew

      #3
      Re: C# seems to lack function that PHP has

      See if this function helps you..

      private bool IsNumeric(strin g strNum)
      {
      try
      {
      Double.Parse(st rNum);
      return true;
      }
      catch
      {
      return false;
      }
      }

      Regards
      Benny

      harrylmh wrote:[color=blue]
      > I want to check if a string "123" is a number. but C# seems to lack a
      > method tat does tat. the closest is char.isNumber where you can only
      > check one character at a time anyway.
      >
      > Anyone knows a way around this? Or is there a method to do this
      > already?
      >
      > Thanks[/color]

      Comment

      • Bill P.

        #4
        Re: C# seems to lack function that PHP has

        You could use a simple regex also:

        return (Regex.IsMatch( inputString, "[0-9]"));

        HTH,
        Bill P.


        "harrylmh" <minghong@hotpo p.com> wrote in message
        news:pcsob05iv3 bou6dqehjc7ti73 de6cubnk3@4ax.c om...[color=blue]
        > I want to check if a string "123" is a number. but C# seems to lack a
        > method tat does tat. the closest is char.isNumber where you can only
        > check one character at a time anyway.
        >
        > Anyone knows a way around this? Or is there a method to do this
        > already?
        >
        > Thanks[/color]


        Comment

        • cody

          #5
          Re: C# seems to lack function that PHP has

          Double.TryParse () will do it.

          --
          cody

          [Freeware, Games and Humor]
          www.deutronium.de.vu || www.deutronium.tk
          "Bill P." <bill@no.spam.c om> schrieb im Newsbeitrag
          news:We3vc.7738 3$mE4.26575@new ssvr25.news.pro digy.com...[color=blue]
          > You could use a simple regex also:
          >
          > return (Regex.IsMatch( inputString, "[0-9]"));
          >
          > HTH,
          > Bill P.
          >
          >
          > "harrylmh" <minghong@hotpo p.com> wrote in message
          > news:pcsob05iv3 bou6dqehjc7ti73 de6cubnk3@4ax.c om...[color=green]
          > > I want to check if a string "123" is a number. but C# seems to lack a
          > > method tat does tat. the closest is char.isNumber where you can only
          > > check one character at a time anyway.
          > >
          > > Anyone knows a way around this? Or is there a method to do this
          > > already?
          > >
          > > Thanks[/color]
          >
          >[/color]


          Comment

          Working...