locked
How to check a character in a string is a numeric or not in Csharp

    Question

  • Hi

    i am using CSHARP code.

    I have a string=" Today is 18-04-2009" like this and how can i check the each character in this string is a valid numeric or not.

     For example like T is not a numeric and 1 is a valid numeric.

    Thanks in advance.
    Saturday, April 18, 2009 1:43 PM

Answers

  • Use the char static methods.
    e.g., char.IsDigit, etc.
    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Proposed as answer by JohnGrove Saturday, April 18, 2009 3:38 PM
    • Marked as answer by Guo Surfer Tuesday, April 21, 2009 10:10 AM
    Saturday, April 18, 2009 1:47 PM
  • Iterate through the string using the foreach loop and check if each character is a number or not.
    Ganesh Ranganathan
    [Please mark the post as answer if you find it helpful]
    • Marked as answer by Guo Surfer Tuesday, April 21, 2009 10:10 AM
    Saturday, April 18, 2009 3:15 PM
  • To piggyback off of David and Ganesh:

    string g = "Today is 18-04-2009";
                foreach (char c in g)
                {
                    if (char.IsDigit(c))
                        Console.WriteLine(c);
                }
                Console.ReadLine();
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Guo Surfer Tuesday, April 21, 2009 10:10 AM
    Saturday, April 18, 2009 3:44 PM

All replies

  • Use the char static methods.
    e.g., char.IsDigit, etc.
    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    • Proposed as answer by JohnGrove Saturday, April 18, 2009 3:38 PM
    • Marked as answer by Guo Surfer Tuesday, April 21, 2009 10:10 AM
    Saturday, April 18, 2009 1:47 PM
  • Iterate through the string using the foreach loop and check if each character is a number or not.
    Ganesh Ranganathan
    [Please mark the post as answer if you find it helpful]
    • Marked as answer by Guo Surfer Tuesday, April 21, 2009 10:10 AM
    Saturday, April 18, 2009 3:15 PM
  • To piggyback off of David and Ganesh:

    string g = "Today is 18-04-2009";
                foreach (char c in g)
                {
                    if (char.IsDigit(c))
                        Console.WriteLine(c);
                }
                Console.ReadLine();
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Marked as answer by Guo Surfer Tuesday, April 21, 2009 10:10 AM
    Saturday, April 18, 2009 3:44 PM