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. how to check if a string is a letter or a number

how to check if a string is a letter or a number

Scheduled Pinned Locked Moved C#
questionhelptutorial
5 Posts 3 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.
  • F Offline
    F Offline
    faladrim
    wrote on last edited by
    #1

    lo; i am reading a string from my comm port, i want to check it wether it is a letter or a number, if it is a letter i need to give an error, if it is a number i want to check if it is smaller then 500 how do i do this? grz & thx

    C G 2 Replies Last reply
    0
    • F faladrim

      lo; i am reading a string from my comm port, i want to check it wether it is a letter or a number, if it is a letter i need to give an error, if it is a number i want to check if it is smaller then 500 how do i do this? grz & thx

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

      You can use a regular expression with the pattern "^\d{1,3}$" to check if the string contains only digits and not more than three digits. If it does, you can safely use int.Parse to parse it to an integer, and check the value.

      --- b { font-weight: normal; }

      F 1 Reply Last reply
      0
      • F faladrim

        lo; i am reading a string from my comm port, i want to check it wether it is a letter or a number, if it is a letter i need to give an error, if it is a number i want to check if it is smaller then 500 how do i do this? grz & thx

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        If it's a char, you can use Char.IsDigit and Char.IsLetter. If it's a full string, then try this: int n; if (int.TryParse(str, out n) && n < 500) { } else { // Not a number, or >= 500 }

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        1 Reply Last reply
        0
        • G Guffa

          You can use a regular expression with the pattern "^\d{1,3}$" to check if the string contains only digits and not more than three digits. If it does, you can safely use int.Parse to parse it to an integer, and check the value.

          --- b { font-weight: normal; }

          F Offline
          F Offline
          faladrim
          wrote on last edited by
          #4

          thx, but i have found something else public bool hasNumber(String s) { for (int j = 0; j < s.Length; j++) { if (!Char.IsDigit(s,j)) { return false; } } return true; } and to know wether it is a letter, just the oppesit :p thx though

          G 1 Reply Last reply
          0
          • F faladrim

            thx, but i have found something else public bool hasNumber(String s) { for (int j = 0; j < s.Length; j++) { if (!Char.IsDigit(s,j)) { return false; } } return true; } and to know wether it is a letter, just the oppesit :p thx though

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

            Yes, that works too. Or even simpler:

            public bool hasNumber(String s) {
            foreach (char c in s) if (!Char.IsDigit(c)) return false;
            return true;
            }

            --- b { font-weight: normal; }

            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