VB.NET Tutorial 7: Strings

In last few tutorials, we learned how to add two variables or how to transfer value of numbers from button to textbox. Similar thing can be done with sentences or what we call in programming sense; a string. Strings are nothing but sequence of characters. Currently what you are reading is also a string. However, in order to tell that we are inputting a string, some preset format is to be followed. Let us learn some basic things about strings in this tutorial.

First of all, create a form with one button and one textbox. Change the name property of button to “Complete String“. Your form must look like this:

form 1

Now double click on button and put following code in it:

Dim FName As String
Dim LName As String
Dim FullName As String

FName = "Gaurav"
LName = "Kulkarni"

FullName = FName & LName
TextBox1.Text = FullName

This code can be easily understood by you except “&” sign. The sentence on the sixth line of code is what new to you. Let us analyze it:

FullName = FName & LName

This line works in similar fashion as that of addition operation of two integer variables. Ampersand sign used in this line is used for joining two strings together. This operation is known as concatenation where two strings are added or attached together. Output of this code is as shown below:

If you can see in output, two words that we inputted through different strings have no space in between. As it doesn’t fit in standard form of writing, change one of the lines in code through which we input value to the string variable. We can change Fname assignment sentence as follows:

Fname = "Gaurav "

We have added a space at the end of the word in double quotations and result will change as follows.

Let us try running the code without putting double quotation marks around values of variables. What happens? You will get an error indicating as shown in image:

If you press “yes” code will still run with indication of two error messages at the bottom. Our program being on smaller scale, won’t create any trouble as of now but putting double quotation marks make task easy for VB and thus produces proper output.

We just learned how to add space between two words that are about to get attached together. Can you imagine of some alternative method? Try to find one and let us know in comments.

To Top