C#

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, 10 November 2013

stringbuilder how it differs from String (Mutable vs Immutable)


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

namespace @string
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("\n---------------------");
            //Console.WriteLine("--CONCAT STRINGS--\n");

            //string s = "Jungle mai jana mana hai...aur gunah hai..";
            //s += "ek gunah nai ,dus gunah hai,hazar gunah hai \n";
            //s=s+ "kyu ki jungle mai \"anu\" or \"malya\" hai";
            //Console.WriteLine("concated string is :{0}",s);
            //Console.WriteLine("\n---------------------");
            //Console.WriteLine("-------we will study about CONCAT(static method)[string.concat]----");

            StringBuilder sA = new StringBuilder("anurag");
            StringBuilder se = new StringBuilder("anurag");
            string sB = "how ", sC = " are ", sD = "you??";
         
            sA.Remove(1, 2);//makes "arag",,,removes 2 character...from position1 that is n

            //sA = sA + se;//wrong
         

            //sA = sA.Append(se);you can...you can also do like below...

            sA.Append(sA);

            //you dont have concat in string builder///


            Console.WriteLine(sA);
            //sA = string.Concat(sA, sB, sC, sD);
            ////string s2= s1.Insert(4, "kk");
            ////Console.WriteLine(s2);
            //Console.WriteLine("the concated string that we got is:{0}",sA);
            Console.ReadLine();

   //------------------------STRING BUILDER[works on instance methods] also check below-------------------------------------//
            StringBuilder sb = new StringBuilder("wow   man");
            sb.Insert(2, "ANU");
            sb.Insert(0, "i am inserting again");
            Console.WriteLine(sb);//i am inserting againwoANUw man
            Console.ReadLine();
        }
    }
}

/*
 There are two type  of methods that we come across...
 * 1)static methods...that is
 * that you get after typing
 * "string."-->compare
 *"string."-->compareordianl
 *"string."-->concat
 *"string."-->copy
 *
 2)Many instance methods....
 * that you get like when you press
 * s.length
 * s.replace
 * s.remove
 * s.toupper
 * s.tolower
 * bla bla bla.....
 *
 *
 *
 * --------------------------------
 * strings are immutable:This means we are going to get new  instance of string object,
 * when we concanate our strings together
 *
 * The SYSTEM.TEXT.STRINGBUILDER ..THIS STRING BUILDER CLASS ALLOWS US TO CONCANATE
 * STRING WITHOUT HAVING TO CREATE NEW OBJECT EACH TIME YOU WANT TO MODIFY THE STRING
 * THIS ALSO GIVES MORE FLEXIBILITY AS WELL




  sA = sA + se;
 Error:Operator '+' cannot be applied to  operands of type 'System.Text.StringBuilder' and 'System.Text.StringBuilder'


          /*
             SUPPOSE THE ABOVE CODE IS WRITTEN LIKE THIS
           
            String sb ="wow man";
            sb.Insert(2, "ANU");
            sb.Insert(0, "i am inserting again");
            Console.WriteLine(sb); //OUTPUT WILL BE "wow man" --[no insertion]
         
            If you want to insert you have to create another instance like this
           
             string s10= sb.Insert(2, "ANU");
             Console.WriteLine(s10);then you get your answer...
           
            */

//you dont have concat in string builder///
 -----------------------------------------------------------------------

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

namespace stringbuilderpart2
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("anurag nayak");
            //sb.Remove(0, 3);


            /*There are two ways to empty the string
             1)remove everything
             2)make the length as zero
           
             */
            //sb.Remove(0, sb.Length);removes everything
            //sb.Length = 0;
            Console.WriteLine(sb);//rag nayak

            sb.Append(" is");
            sb.Append(" a");
            sb.Append(" boy");

            Console.WriteLine(sb);
         


            Console.WriteLine("the capacity of the string is:{0}",sb.Capacity);
            Console.WriteLine("the length of the string is:{0}", sb.Length);


         


            Console.ReadLine();
             


        }
    }
}
/*
 This program is very interesting.....
 * Because you will see that....capacity > length.....
 *
 * the lenght is :21
 * the capacity is :32
 * so 32 bytes are allocated....if you increase the length ....more than 32
 * the capacity is automatically resizes itself as 64

 */





No comments:

Post a Comment