Results 1 to 6 of 6

Thread: Difference between string and stringbuilder?

  1. #1
    Join Date
    Nov 2009
    Posts
    55

    Difference between string and stringbuilder?

    I am attending lecture for C# and it is new language for me. I can not understand difference between string stringbuilder. I know this terms are related to the string but this information is not enough. Someone help me to understand string and stringbuilder. Please give me some example that based on it.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Difference between string and stringbuilder?

    Code:
    Imports System.Text
     
    public class Test
    
       public Shared Sub Main
            Const ADD As String = "1234567890"
            Dim num As Long = 1000
            Dim start As DateTime
            Dim stop As DateTime
            Dim elapsed As TimeSpan
            Dim txt1 As String
            Dim string_builder As New StringBuilder
    
            txt = ""
            start = Now
            For i As Long = 1 To num
                txt1 = txt1 & ADD
            Next i
            stop= Now
            elapsed = stop.Subtract(start)
            Console.WriteLine(elapsed.TotalSeconds.ToString("0.000000"))
    
            txt = ""
            start = Now
            For i As Long = 1 To num
                string_builder.Append(ADD)
            Next i
            txt1 = string_builder.ToString()
            stop = Now
            elapsed = stop.Subtract(start)
            Console.WriteLine(elapsed.TotalSeconds.ToString("0.000000"))
    
       End Sub
    
    End class

    This code will help you.

  3. #3
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Difference between string and stringbuilder?

    Difference between String ans stringbuilder is as follows-
    string-
    1.string is handled using this class.
    2.concatenation method is used to concatenate two strings.
    3.For concatenating two string string object is used.
    4.Strings are immutable.

    String Builder-
    1.This is also the class used to handle strings.
    2.Append method is used to concatenate two strings.
    3.Stringbuilder object is used.
    4.Existing string is used for insertion.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Difference between string and stringbuilder?

    String are Immutable. If you try to change the string then it creates a new string and the old string will be ready for garbage collection. String is immitable, StringBuilder is mutable. When StringBuilder instantiated, it make a new string with predefined capacity and it can accodomate string without creating new memory location for the string.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Difference between string and stringbuilder?

    Once a string object is created then they cannot changed hence they called immutable. If we used one of the method of a string class then new string object is created.

    For example: String strSen = “hello”;
    When we try to concatenate any other words along with strSen variable, it does not modify the strSen variable, instead of it creates a new string.
    For example: strSen = strSen + “Hello”;

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Difference between string and stringbuilder?

    In string concatenation method StringBuilder provide better performance than string object. StringBuilder is mutable class which allow to modify the original value without creating a new object. You can use StringBuilder Append() method for concatenation.

Similar Threads

  1. Differentiation among StringBuffer and StringBuilder class
    By Ram Bharose in forum Software Development
    Replies: 5
    Last Post: 27-01-2010, 02:20 PM
  2. Difference between String and StringBuffer.
    By Luz in forum Software Development
    Replies: 3
    Last Post: 01-12-2009, 10:13 PM
  3. How to Manipulate String using PHP String Functions
    By ComPaCt in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 09:07 AM
  4. VB.NET StringBuilder
    By Chrisch in forum Software Development
    Replies: 3
    Last Post: 11-06-2009, 08:34 AM
  5. Difference between capacity and size functions of string class
    By DimitrisLiatsos in forum Software Development
    Replies: 5
    Last Post: 25-10-2008, 05:07 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,900,992.79787 seconds with 16 queries