In Visual Basic 2015, a string is a single unit of data that made up of a series of characters that includes letters, digits, alphanumeric symbols(@,#,$,%,^,&,*, etc) and more. It is treated as the String data type and therefore it is non-numeric in nature, though it might consist of numbers. Everyday life examples of strings are names, addresses, gender, cities, book titles, phone numbers, email addresses and more.In Visual Basic 2015, you can manipulate strings by writing code to process characters like sentences, words, text , ” ,alphanumeric characters and more. Strings manipulation is best illustrated in the area of word processing that deals with text editing.
In Visual Basic 2015, you can manipulate strings 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 “Vis”,”Basic” and “2015″ into “Visual Basic 2015″ using “Visual”&”Basic” or “Visual “+”Basic”, as shown in the Examples below:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim str1 = "Visual ", str2 = "Basic ", str3 = "2015", str As String str = str1 + str2 + str3 MsgBox(str) End Sub
The line str = str1 + str2 + str3 can be replaced by str = str1 & str2 & str3 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.
The output is shown in Figure 12.1:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.LoadDim str1 = "Visual ", str2 = "Basic ", str3 = "2015", str As String Dim str4 As Integer str4 = 100 str = str1 + str2 + str3 + str4 MsgBox(str) End Sub
This code will produce an error because of data mismatch. The error message appears as shown in Figure 12.2.
However, using & instead of + will fix the error as the integer will be treated as a string. The output is as follows:
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 that are built into Visual Basic 2015.
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 2015) = 17 and Len (“welcome to VB 2015 tutorial”) = 27
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim MyText as String MyText="Visual Basic 2015" MsgBox(Len(MyText)) End Sub
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 MyText As String MyText="Visual Basic" MsgBox(Microsoft.VisualBasic.Right(MyText, 4)) End Sub
The above program returns four rightmost characters of the phrase entered into the textbox.
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 will be extracted. For example,
Microsoft.VisualBasic.Left (“Visual Basic”, 4) = Visu
The Mid function is used to retrieve a part of text from a given phrase. The syntax of the Mid Function is
phrase is the string from which a part of the text is to be retrieved. position is the starting position of the phrase from which the retrieving process begins. n is the number of characters to retrieve.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myPhrase As String myPhrase = InputBox("Enter your phrase") LblPhrase.Text = myPhrase LblExtract.Text = Mid(myPhrase, 2, 6) End Sub
* In this example, when the user clicks the button, an input box will pop up prompting the user to enter a phrase. After a phrase is entered and the OK button is pressed, the label will show the extracted text starting from position 2 of the phrase and the number of characters extracted is 6.
The Trim function trims the empty spaces on both sides of the phrase. The syntax is
Trim (” Visual Basic “) = Visual basic
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myPhrase As String myPhrase = InputBox(“Enter your phrase”) Label1.Text = Trim(myPhrase) End Sub
The Ltrim function trims the empty spaces of the left portion of the phrase. The syntax is
Ltrim (” Visual Basic 2015″)= Visual basic 2015
The Rtrim function trims the empty spaces of the right portion of the phrase. The syntax is
Rtrim (“Visual Basic 2015 “) = Visual Basic 2015
The InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The syntax is
Instr (n, original phase, embedded phrase)
Where n is the position where the Instr function will begin to look for the embedded phrase. For example
Instr(1, “Visual Basic 2015 “,”Basic”)=8
*The function returns a numeric value.
You can write a program code as shown below:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = InStr(1, “Visual Basic”, “Basic”) End Sub
The Ucase function converts all the characters of a string to capital letters. On the other hand, the Lcase function converts all the characters of a string to small letters.
The syntaxes are
Microsoft.VisualBasic.Ucase(“Visual Basic”) =VISUAL BASIC Microsoft.VisualBasic.Lcase(“Visual Basic”) =visual basic
The Chr function returns the string that corresponds to an ASCII code while the Asc function converts an ASCII character or symbol to the corresponding ASCII code. ASCII stands for “American Standard Code for Information Interchange”. Altogether there are 255 ASCII codes and as many ASCII characters. Some of the characters may not be displayed as they may represent some actions such as the pressing of a key or produce a beep sound. The syntax of the Chr function is
and the syntax of the Asc function is
The following are some examples:
Chr(65)=A, Chr(122)=z, Chr(37)=% , Asc(“B”)=66, Asc(“&”)=38