In Visual Basic 2012 , string manipulation is important because it helps to process data that come in the form of non-numeric types such as names, addresses, gender, cities, book titles and more.
In Visual Basic 2012, strings can be manipulated using the & sign and the + sign, both perform the string concatenation which means combining two or more smaller strings into larger strings. For example, we can join "Visual" ,"Basic" and "2012" into "Visual Basic 2012" using "Visual"&"Basic" or "Visual "+"Basic", as shown in the Examples below
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim text1, text2, text3, text4 As String text1 = "Visual" text2 = "Basic" text3="2012" text4 = text1 + text2+text3 MsgBox(text4) End Sub End Class
The line text4=text1+ text2 + text3 can be replaced by text4=text1 & text2 &text3 and produces the same output. However, if one of the variables is declared as numeric data type, you cannot use the + sign, you can only use the & sign.
Dim text1, text3 as string Dim Text2 As Integer text1 = "Visual" text2=22 text3=text1+text2 Label1.Text = text3
This code will produce an error because of data mismatch.However, using & instead of + will be all right.
Dim text1, text3 as string Dim Text2 As Integer text1 = "Visual" text2=22 text3=text1 & text2 Label1.Text = text3
You can combine more than two strings to form a larger string, like the following example:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim text1, text2, text3, text4, text5, text6 As String text1 = "Welcome" text2 = " to" text3 = " Visual" text4 = " Basic" text5 = " 2012" text6 = text1 + text2 + text3+text4+text5 Label1.Text = text6 End Sub End Class
Running the above program will produce the following screen shot, as shown in Figure 8.1.
A function is similar to a normal procedure but the main purpose of the function is to accept a certain input and return a value which is passed on to the main program to finish the execution.There are numerous string manipulation functions built into Visual Basic 2012 but I will only discuss a few here and will explain the rest of them in later lessons.
The Len function returns an integer value which is the length of a phrase or a sentence, including the empty spaces. The syntax is
Len (Visual Basic) = 12 and Len ("welcome to VB tutorial") = 22
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = Len(TextBox1.Text) End Sub End Class
The Right function extracts the right portion of a phrase. The syntax is
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim text1 As String Label1.Text = Microsoft.VisualBasic.Right(text1, 4) End Sub
The above program returns four right most characters of the phrase entered into the textbox.
The Output is shown in Figure 8.3
*The reason of using the full reference is because many objects have the Right properties so using Right on its own will make it ambiguous to Visual Basic 2012.
The Left function extracts the left portion of a phrase. The syntax is
Where n is the starting position from the left of the phase where the portion of the phrase is going to be extracted. For example,
Microsoft.VisualBasic.Left ("Visual Basic", 4) = Visu