ARTICLE

Copy and Concatenate String in VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language August 25, 2010
This code snippet shows how to copy and concatenate strings in VB.NET.
 
Reader Level:

The Concat method adds strings (or objects) and returns a new string. Using Concat method, you can add two strings, two objects and one string and one object or more combination of these two.

The following source code concatenate two strings.

    Dim str1 As String = "ppp"
    Dim str2 As String = "ccc"
    Dim strRes As String = [String].Concat(str1, str2)
    Console.WriteLine(strRes)

The following source code concatenates one string and one object.

    Dim str1 As String = "ppp"
    Dim strRes As String
    Dim
obj As Object = DirectCast(12, Object)
    strRes =
[String].Concat(str1, obj)
    Console.WriteLine(strRes)

The Copy method copies contents of a string to another. The Copy method takes a string as input and returns another string with the same contents as the input string. For example, the following code copies str1 to strRes.

    Dim str1 As String = "ppp"
    Dim strRes As String = [String].Copy(str1)
    Console.WriteLine("Copy result :" & strRes)

The CopyTo method copies a specified number of characters from a specified position in this instance to a specified position in an array of characters. For example, the following example copies contents of str1 to an array of characters. You can also specify the starting character of a string and number of characters you want to copy to the array.

    Dim str1 As String = "ppp"
    Dim chrs As Char() = New [Char](1) {}
    str1.CopyTo(0, chrs, 0, 2)
    Console.WriteLine(chrs)

The Clone method returns a new copy of a string in form of object. The following code creates a clone of str1.

    Dim str1 As String = "ppp"
    Dim objClone As Object = str1.Clone()
    Console.WriteLine("Clone :" & objClone.ToString())

The Join method is useful when you need to insert a separator (String) between each element of a string array, yielding a single concatenated string. For example, the following sample inserts a comma and space (", ") between each element of an array of strings.

    Dim str1 As String = "ppp"
    Dim str2 As String = "ccc"
    Dim str3 As String = "kkk"
    Dim allStr As String() = New [String]() {str1, str2, str3}
    Dim strRes As String = [String].Join(", ", allStr)
    Console.WriteLine("Join Results: " & strRes)

Conclusion

Hope this article would have helped you in understanding different string methods in VB.NET.

Login to add your contents and source code to this article
share this article :
post comment
 

Thank u very much. This article helped me a lot for my final year exam.

Posted by banu menon Apr 24, 2012
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor