Home » VB.NET Basics » 05 - Strings
5

Blanks

Handling blanks in text variables

Sometimes you will encounter blanks with text variables. For example, if a user enters the name " Jordan" instead of "Jordan," there will be a space or a blank in front of it. If you want to rid the blanks at the start and at the end of text entered by the user then you can use the trim method. Simply use the code as illustrated below to trim any string variables.


        Dim Name As String

        Name = Trim(TextBox1.Text)

        MsgBox (Name)
		

As you can see, simply enter the text in the Trim() function and the spaces at the start and at the end of the text variable will be gone.

Note: Sometimes users will forget to put an entry in a textbox if you ask them to. In order to recognize blanks, simply use "". Note that " " is different from "" because the former has a space bar entry while the latter has nothing in it. Visual Basic .NET recognizes the difference between the two.