Friday, 18 March 2016

String Operation in C#

String Operations :
1. Get the Substring from a string
2. Reverse a string
3. Concatenate a string
4. Check whether a string contains a particular string or not
5. Compare two strings

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

namespace StringOperation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("String Operations");
            Console.WriteLine();
            Console.WriteLine("1. Get the substring from a string.");
            string s1 = "Dream big and dare to fail, ";
            string s2 = "Never give up on your Dream.";          
            string get = s1.Substring(0,6);
            Console.WriteLine("String is : {0}", s1);
            Console.WriteLine("Substring of this String is : {0}",get);
            Console.WriteLine(); Console.WriteLine();
           
            Console.WriteLine("2. Reverse a String.");
            string str = "Damon Salvatore";
            Console.WriteLine("Original String is : {0}",str);
            char[] cArray = str.ToCharArray();
            string reverse = String.Empty;
            for (int i = cArray.Length - 1; i > -1; i--)
            {
                reverse += cArray[i];
            }
            Console.WriteLine("After reverse the String is : {0}",reverse);
            Console.WriteLine(); Console.WriteLine();

            Console.WriteLine("3. Concatinate a String");
            string concat = s1 + s2;
            Console.WriteLine("After Concatination the string is : ");
            Console.WriteLine(">>> {0}",concat);
            Console.WriteLine(); Console.WriteLine();

            Console.WriteLine("4. Check whether a string contain a particular string.");
            if (s1.Contains("Dream"))
                Console.WriteLine("Dream does contain in string.");
            else
                Console.WriteLine("It doesnot contain.");
            Console.WriteLine(); Console.WriteLine();

            Console.WriteLine("5. Compare two string.");
            s1 = "Ramandeep";
            s2 = "Ramandeep";
            if(s1==s2)
                Console.WriteLine("Is equal");
            else
                Console.WriteLine("Is not equal");
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment

Read contacts in iOS using Swift with error handling

Just copy and paste the below code Pre-step before pasting the below code Add below line in AppDelegate.swift file below import statement ...