ARTICLE

Add, Remove and Replace Strings in VB.NET

Posted by Mahesh Chand Articles | Strings, Arrays in VB.NET August 25, 2010
This code snippet shows how to add, remove and replace strings in VB.NET.
 
Reader Level:

The Insert method inserts a specified string at a specified index position in an instance. For example, the following source code inserts "bbb" after second character in str1 and the result string is "pbbbpp".

    Dim str1 As String
    Dim
StrRes As String
    str1 = "ppp"
    strRes = str1.Insert(2, "bbb")
    Console.WriteLine(StrRes.ToString())

The Remove method deletes a specified number of characters from a specified position in a string. This method returns result as a string. For example, the following code removes three characters from index 3.

    Dim s As String = "123abc000"
    Console.WriteLine(s.Remove(3, 3))

The Replace method replaces all occurrences of a specified character in a string. For example, the following source code replaces all p character instances of str1 with character l and returns string "lll".

    Dim str1 As String = "ppp"
    Dim repStr As String = str1.Replace("p"c, "l"c)
    Console.WriteLine("Replaced string:" & repStr.ToString())

The Split method separates strings by a specified set of characters and places these strings into an array of strings. For example, the following source code splits strArray based on ',' and stores all separated strings in an array.

    Dim str1 As String = "ppp"
    Dim str2 As String = "ccc"
    Dim str3 As String = "kkk"
    Dim strAll3 As String = str1 & ", " & str2 & ", " & str3
    Dim strArray As String() = strAll3.Split(","c)
    For Each itm As String In strArray
        Console.Write(itm.ToString())
    Next

Conclusion

Hope this article would have helped you in understanding add, remove and replace strings in VB.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
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. Visit DynamicPDF here
    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.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor