Thursday, May 9, 2013

C# -How to use regular expression


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //creating arraylist
            ArrayList arrlist = new ArrayList();
            //
            // String containing numbers.
            //
            Console.WriteLine("Enter a Line ");
            string sentence = Console.ReadLine(); //"10 cats, 20 dogs, 40 fish and 1 programmer.";
            //
            // Get all digit sequence as strings.
            //
            string[] digits = Regex.Split(sentence, @"\D+");
            //
            // Now we have each number string.
            //
            foreach (string value in digits)
            {
                //
                // Parse the value to get the number.
                //
                int number;
               // if (int.TryParse(value, out number))
                {
                    arrlist.Add(value);
                    Console.WriteLine(value);
                }
            }
            Console.WriteLine("Using Array-----");
            foreach (object ob in arrlist)
            {
                Console.WriteLine(ob.ToString());
            }
            Console.ReadLine();


            //**********************************************
            // See if we can parse the 'text' string. If we can't, TryParse
            // will return false. Note the "out" keyword in TryParse.
            //
            string text1 = "x";
            int num1;
            bool res = int.TryParse(text1, out num1);
            if (res == false)
            {
                // String is not a number.
            }

            //
            // Use int.TryParse on a valid numeric string.
            //
            string text2 = "10000";
            int num2;
            if (int.TryParse(text2, out num2))
            {
                // It was assigned.
            }

            //
            // Display both results.
            //
            Console.WriteLine(num1);
            Console.WriteLine(num2);
            Console.ReadLine();
            Console.WriteLine("End of program");


        }
    }
}

No comments:

Post a Comment

Your comment is pending for approval

AngularJS Basics - Part 1

                                                                  AngularJS What is AngularJS ·          Framework by googl...