Friday, May 7, 2021

C# Utilities Methods

Utilities Methods

  1. Convert Array into string
  2. Convert string into Array
  3. Convert string type number into number
  4. Convert string type date into DateTime date
  5. String comparison
  6. Find first animal which names begin with C
  7. Find last animal which names begin with C
  8. Find all animals which names begin with C
  9. Sort Array
  10. Reverse Array
  11. Get distinct array elements


using System;

namespace ConsoleUtilities

{

    class Test

    {

        public void Utilities()

        {

            //Q1. Convert Array into string

            string[] animals = {"Rat", "Cat","Dog","Fox","Lion" };

            string a2s = String.Join(",", animals);

            Console.WriteLine(a2s);

            //Q2. Convert string into Array

            string quote = "It is not a lack of love, but a lack of friendship that makes unhappy marriages.";

            char delimiter = ' ';

            string[] s2a = quote.Split(delimiter);

            foreach (var q in s2a)

            {

                Console.WriteLine(q);

            }

            //Q3. Convert string type number into number

            string intNum = "123";

            string dblNum = "123.45";

            int num = Convert.ToInt32(intNum);

            double dbl = Convert.ToDouble(dblNum);

            Console.WriteLine(num);

            Console.WriteLine(dbl);

            //Q4. Convert string type date into DateTime date

            string date_str  = "11/11/2021";

            DateTime date = Convert.ToDateTime(date_str);

            Console.WriteLine(date.ToString("D"));

            //Q5. String comparison

            string city = "London";

            Console.WriteLine(city.Equals("london"));//False

            Console.WriteLine(city == "london");//False

            Console.WriteLine(city.Equals("London"));//True

            Console.WriteLine(city == "London");//True

                                Console.WriteLine(city.Equals("london",StringComparison.CurrentCultureIgnoreCase));//True

            //Q6. Search an array element

            string[] animates = { "Rat", "Cat", "Snake","Dog", "Fox", "Lion","Tiger","Bear","Bull","Cow","Rhino"};

            var result = Array.Find(animates, s=>s== "dog"); //2nd arg is System.Predicate<String>, case senstive

            if (result==null)

            {

                Console.WriteLine("Dog not found.");

            }

            else

            {

                Console.WriteLine(result);

            }

            //Q7. Search an array element

            // Find first animal which names begin with C

            var r1 = Array.Find(animates, s => s.StartsWith("C"));

                Console.WriteLine(r1);//Cat

            // Find last animal which names begin with C

            var r2 = Array.FindLast(animates, s => s.StartsWith("C"));

            Console.WriteLine(r2);//Cow

            //Q8. Find all animals which names begin with C

            var allRes = Array.FindAll(animates, s => s.StartsWith("R"));

            foreach (var r in allRes)

            {

                Console.WriteLine(r);

            }

            //Q9. Sort Array

            Array.Sort(animates); //Sort returns void type

            foreach (var a in animates)

            {

                Console.Write("{0} < ",a);

            }

            //Q10. Reverse Array

            Array.Reverse(animates);

            Console.WriteLine();

            for (int i = 0; i < animates.Length; i++)

            {

                Console.Write("{0} > ",animates[i]);

            }

            //Q11. Get distinct array elements

            Console.WriteLine();

            string[] myarr = {"Bear", "Bull", "Cow", "Rhino", "Bear", "Ox"};

            //we use LINQ to get the result, so use System.Linq namespace

            var distinct = myarr.Distinct();

            foreach (var item in distinct)

            {

                Console.Write("{0} ",item);

            }

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Test t = new Test();

            t.Utilities();

            Console.ReadKey();

        }

    }

}

OUTPUT

Rat,Cat,Dog,Fox,Lion

It

is

not

a

lack

of

love,

but

a

lack

of

friendship

that

makes

unhappy

marriages.

123

123.45

November 11, 2021

False

False

True

True

True

Dog not found.

Cat

Cow

Rat

Rhino

Bear < Bull < Cat < Cow < Dog < Fox < Lion < Rat < Rhino < Snake < Tiger <

Tiger > Snake > Rhino > Rat > Lion > Fox > Dog > Cow > Cat > Bull > Bear >

Bear Bull Cow Rhino Ox

 

No comments:

Post a Comment

Hot Topics