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. String containing digit or Alphabet

String containing digit or Alphabet

Scheduled Pinned Locked Moved C#
question
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
    t4ure4n
    wrote on last edited by
    #1

    I have a variable string input How do i detect if variable has digit in it or an alphabet.

    o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

    L L C 3 Replies Last reply
    0
    • T t4ure4n

      I have a variable string input How do i detect if variable has digit in it or an alphabet.

      o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      t4urean wrote:

      How do i detect if variable has digit in it or an alphabet.

      In terms of an exclusive or? To check, if the string is a number: int i = int.TryParse(input) If it's just one digit: int i = int.TryParse(input) and then i =< 9 && i >= 0 If it contains one or more alphabets: bool b = Regex.IsMatch(input, "[a-zA-Z]") regards

      1 Reply Last reply
      0
      • T t4ure4n

        I have a variable string input How do i detect if variable has digit in it or an alphabet.

        o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, you can walk the individual characters of the string:

        for (int i=0; i

        or equivalently (unless you want to change c, wouldnt work here):

        foreach (char c in input) {
        ... do something with c
        }

        and you can use one of many many methods available in String class,
        such as IndexOf() and Contains()

        May I suggest you at least read the MSDN documentation on String class;
        or better yet work your way through a introductory C# book.

        :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] }
        catch { [Google] }


        1 Reply Last reply
        0
        • T t4ure4n

          I have a variable string input How do i detect if variable has digit in it or an alphabet.

          o O º(`'·.,(`'·., ☆,.·''),.·'')º O o° »·'"`»* *☆ t4ure4n ☆* *«·'"`« °o O º(,.·''(,.·'' ☆`'·.,)`'·.,)º O o°

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

          You can use regex, or iterate over the chars and use things like char.IsDigit.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          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