String Functions

Trailing spaces

Sometimes, you will find you need to remove trailing spaces from a string. There are 3 functions for this. Trim, LTrim, RTrim. Trim removes spaces from either end of a string. LTrim removes spaces from the left end of a string. RTrim removes spaces from the right end of a string. They all use the following syntax:

Trim(String)

The following code fills String1 with '    Hello World    ', then fills Text1, Text2, Text3, Text4 displaying the results of using nothing, Trim, LTrim and RTrim respectively

'// Declare the variable
Dim sString As String
'// Fill the variable
sString = "  Hello World   "
'// Display plain string
Text1.Text = sString
'// Display string using Trim
Text2.Text = Trim(sString)
'// Display string using LTrim
Text3.Text = LTrim(sString)
'// Display string using RTrim
Text4.Text = RTrim(sString)

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An expert is a man who has made all the mistakes that can be made in a very narrow field” - Niels Bohr