Concatenat Strings in VB.NET

In this article we use a new class StringBuilder to perform string concatenation.
  • 1410

We can concatenate string Vb.Net by using '&' operator but there is a drawback of this procedure of concatenation that on every concatenation, a string variable was created and the old one was destroyed. So, Vb.Net provided a new class called StringBuilder to resolving the old problem. The StringBuilder dynamically allocates the memory block to the same object. It resides in the system.text namespace.

Here is the Example of using StringBuilder class

    Imports System.Text
    Module Module1
        Sub Main()
            Dim concatenation As New StringBuilder()
            concatenation.Append(
"Manish ")
            concatenation.Append(
"is student ")
            concatenation.Append(
"of ")
            concatenation.Append(
"NIIT.")
            Dim myString As New String(concatenation.ToString())
            Console.WriteLine(concatenation)
            Console.ReadLine()
        End Sub
    End
Module

OUTPUT

7.gif

CONCLUSION

So, in this article you see two separate objects, first is StringBuilder which take care of memory management and the another is .toString() which is responsible for the result output production.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.