Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. determining if a string contains an int

determining if a string contains an int

Scheduled Pinned Locked Moved C#
csharpdotnetquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tyrus182
    wrote on last edited by
    #1

    is there any way within the .net framework to determine if a string contains a numeric value?

    G A M 3 Replies Last reply
    0
    • T Tyrus182

      is there any way within the .net framework to determine if a string contains a numeric value?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      The easiest way is to put a try...catch around the conversion:

      try {
      num = int.Parse(str);
      ok = true;
      } catch {
      ok = false;
      }

      Exceptions are quite expensive, though, so it's more efficient to validate the string before you convert it. You can use the Regex class to check if the string only contains digits, for an example. --- b { font-weight: normal; }

      1 Reply Last reply
      0
      • T Tyrus182

        is there any way within the .net framework to determine if a string contains a numeric value?

        A Offline
        A Offline
        Alsvha
        wrote on last edited by
        #3

        do you mean if the string is a number, or can contain a number somewhere amongst a lot of other characters? For the first, if you want to figure out if a string is a number, I'd use Double.TryParse(string) to figure this out (returns a boolean). (Much more effective then "try-catch"ing) For the latter, I'd use regular expression to match any digits (\d) and then it'll return boolean. (regular expression can also subtract the numbers it finds, if needs be...) I hope this help. --------------------------- 127.0.0.1 - Sweet 127.0.0.1 -- modified at 3:18 Monday 5th September, 2005

        1 Reply Last reply
        0
        • T Tyrus182

          is there any way within the .net framework to determine if a string contains a numeric value?

          M Offline
          M Offline
          Mark Greenwood
          wrote on last edited by
          #4

          [Message Deleted]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups