Home » VB.NET Basics » 05 - Strings
5

Joining, replacing and inserting text

Joining Text

Aside from using the "=" sign from numerical values, you can also use the "+" sign too. The "+" sign is used to join text together or to concatenate them. If you want to join text together, simply use the "+" sign as seen below.


Dim Result As String

Result = "Sun" + "day"

MsgBox(Result)
		

The result in the message box is the word "Sunday". Try joining different kinds of texts together. The basic structure for joining text is as follows.

 [first string] + [second string] 

Replacing Text

If you want to replace a certain string or substring then you can use the Replace() command. See the code below and learn how it is used.


Dim Represent As String

Represent = "Display".Replace("Display", "Screen")

MsgBox(Represent)

The entire word "Display" is replaced with the word "Screen". Try changing Replace("Display", "Screen") with Replace("ispl","") and see what happens. The general structure of the Replace command is as follows.


"[string to be looked at]".Replace("[string to be replaced]","[string to replace it]")

Inserting Text

If you wish to insert a string of text into an existing string then you can use the Insert() command. See the code below and learn how it is used.


        Dim Represent As String

        Represent = "Day".Insert(1, "ispl")

        MsgBox(Represent)

The result that is displayed in the message box is "Display". Note that the first character in the String is "D" and that is why the "ispl" string was inserted after the character "D". Try changing Insert(1, "ispl") to Insert(2, "ispl") and see what happens. The general structure of the Insert command is as follows.

 "[string to be looked at]".Insert([nth character where string will be inserted], "[string to be inserted]")