In Visual Basic 2022, a string is a single unit of data that made up of a series of characters that include letters, digits, alphanumeric symbols(@,#,$,%,^,&,*, etc) and more. It is treated as the String data type. 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 2022, you can manipulate strings by writing code to process characters like sentences, words, text , alphanumeric characters and more.
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 "Visual","Basic" and "2022" into "Visual Basic 2017" 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 = "2022", 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.Load Dim str1 = "Visual ", str2 = "Basic ", str3 = "2022", 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, 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 2022.
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 2022) = 17 and Len ("welcome to vb 2022 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 2022" MsgBox("The Length of" & MyText & "is" & 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("The four rightmost characters of " & myText & " is " & 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.
Dim MyText As String MyText="Visual Basic" MsgBox("The four rightmost characters of " & myText & " is " & Microsoft.VisualBasic.Left(MyText, 4))
The Mid function is used to extract a portion of text from a given phrase. The syntax of the Mid Function is
where phrase is the string from which a part of the text is to be extracted, position is the starting position of the phrase from which the extraction process begins. Lastly, n is the number of characters to extract.
In this example, we insert two Labels, one to display the phrase entered and the other one to display the extracted phrase. Besides that, we add two text boxes, one of them is for the user to enter the position to start extraction while the other one is for the user to enter the number of characters to be extracted. In addition, insert two buttons, one is for the user to click and enter the phrase in the InputBox while another one for displaying the extracted phrase. The code is as follows:
Public Class Form1 Dim myPhrase As String Dim pos As String Dim num As String Private Sub BtnEnter_Click(sender As Object, e As EventArgs) Handles BtnEnter.Click myPhrase = InputBox("Enter your phrase") LblPhrase.Text = myPhrase End Sub Private Sub BtnExtract_Click(sender As Object, e As EventArgs) Handles BtnExtract.Click pos = TxtPos.Text num = TxtNum.Text LblExtract.Text = Mid(myPhrase, pos, num) End Sub End Class
* 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 basicExample 12.4
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 2022″)= Visual basic 2022
The Rtrim function trims the empty spaces of the right portion of the phrase. The syntax is
Rtrim ("Visual Basic 2022 ") = Visual Basic 2022
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 2022 ","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