Strings manipulation - C# Tutorials

Latest

For Video tutorials Visit my Channel https://www.youtube.com/channel/UCVWpZVbMqLHuQBigIfc9I8Q

Tuesday, 14 February 2017

Strings manipulation

This tutorial show operations that can be perform on string.


using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace StringApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int op;
            Console.WriteLine("Enter 1 for String concatenation ");
            Console.WriteLine("Enter 2 for using string constructor ");
            Console.WriteLine("Enter 3 for methods returning string ");
            Console.WriteLine("Enter 4 for formatting method to convert a value ");
            Console.WriteLine("Enter 5 for Comparing String function");
            Console.WriteLine("Enter 6 for String contains String function");
            Console.WriteLine("Enter 7 for Getting a SubString function");
            Console.WriteLine("Enter 8 for Joining Strings function");
            Console.WriteLine("Enter 9 for Exit");
            Console.WriteLine("Enter your choice");
            op = Convert.ToInt16(Console.ReadLine());
            Console.Clear();
            switch(op)
            {
                  case 1:
                      {
                    //from string literal and string concatenation
                    string fname, lname;
                    fname = "Basit";
                    lname = "Jamil";

                    string fullname = fname + lname;// concate two strings
                    Console.WriteLine("Full Name: {0}", fullname);
                    break;
                     }

                case 2:
                    {
                        //by using string constructor
                        char[] letters = { 'H', 'e', 'l', 'l', 'o' };
                        string greetings = new string(letters);
                        Console.WriteLine("Greetings: {0}", greetings);
                        break;
                    }
                case 3:
                    {
                        //methods returning string
                        string[] sarray = { "Hello", "world", "C# " };
                        string message = String.Join(" ", sarray);
                        Console.WriteLine("Message: {0}", message);
                        break;
                    }
                case 4:
                    {
                        //formatting method to convert a value
                        DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
                        string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
                        Console.WriteLine("Message: {0}", chat);
                        break;
                    }
                case 5:
                    {
                        string str1 = "This is test";
                        string str2 = "This is text";

                        if (String.Compare(str1, str2) == 0)
                        {
                            Console.WriteLine(str1 + " and " + str2 + " are same.");
                        }
                        else
                        {
                            Console.WriteLine(str1 + " and " + str2 + " are not same.");
                        }
                        break;
                    }
                case 6:
                    {
                        string str = "This is test";
                        if (str.Contains("test"))
                        {
                            Console.WriteLine("asdfads .");
                        }
                        break;
                    }
                case 7:
                        {
                            string str = "abc ";
                            Console.WriteLine(str);
                            string substr = str.Substring(23);
                            Console.WriteLine(substr);
                            break;
                        }
                case 8:
                    {
                        string[] starray = new string[]{"aoa",
                                                        "w/s",
                                                        "ok",
                                                        "h r u",
                                                        "hellow"};

                        string str = String.Join("\n", starray);
                        Console.WriteLine(str);
                        break;
                    }
                case 9:
                    {
                        System.Environment.Exit(1);
                        break;
                    }

            }
            Console.ReadKey();
        }
    }

}

No comments:

Post a Comment