Sponsored Ad

Monday, May 10, 2010

Check Characters of String for All Digits in C#

 

This program will help you to check character by character string that it contains the digits or not.

There is separate method to check this functionality , you can copy and paste to your program to use it.

Digits along with Char - C#

Digits along with No Char - C#

Source code:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyFirstConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is my First Console Application");
            // user input
            Console.WriteLine("Please enter some string:");
            string strInput = Console.ReadLine();
            Console.WriteLine("you have entered a string: " + strInput);
            Console.WriteLine();
            bool blnResults = CheckForDigits(strInput);
            strInput = blnResults?"Yes":"No";
            Console.WriteLine("String contains the all digits? " + strInput);

            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }

        public static bool CheckForDigits(string strTemp)
        {
            //remove white spaces
            strTemp = strTemp.Trim();

            if (strTemp.Length == 0)
            {
                return false;
            }
            // loop through the string
            for (int i = 0; i < strTemp.Length; i++)
            {
                if (Char.IsDigit(strTemp[i]) == false)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates