Use the LTrim and RTrim Functions, which remove spaces from the left and right.
LTrim, RTrim. LTrim and RTrim are available in VB.NET. What does the LTrim function do? The L here stands for Left—the left trim method removes spaces from the left of the String passed to it. RTrim meanwhile removes from the right side of String data.Strings
LTrim. In this example, we declare a String literal with three spaces before the first letter character. Then, we pass the variable that points to that String literal to the LTrim function. The string value returned has no leading spaces on it.
VB.NET program that uses LTrim function
Dim value1 As String = " Dot Net Perls"
Dim value2 As String = LTrim(value1)
[Dot Net Perls]
How is the LTrim function implemented? To investigate, I opened the Microsoft.VisualBasic.dll file with IL Disassembler. I found that, internally, LTrim calls TrimStart. So you can think of LTrim as a wrapper method for TrimStart.
But: It would be more efficient to simply call TrimStart yourself. This is possible without too much trouble.
RTrim. Let's get started with RTrim. A String literal with several trailing spaces is declared and the value1 variable points to it. When this variable is passed to RTrim, another String is returned.
Output: We can see that there are no trailing spaces remaining on the right side of the string value.
Note: The original String literal is unchanged, but changes are made to another copy which is displayed.
VB.NET program that calls RTrim
Dim value1 As String = "Dot Net Perls "
Dim value2 As String = RTrim(value1)
[Dot Net Perls]
Internally, RTrim simply calls TrimEnd on the String type. TrimEnd is a form of Trim that only acts on the trailing (right-side) characters. It would be possible, and usually preferable, to directly call TrimEnd.
Summary. We looked at the LTrim function in the VB.NET language and saw both its effect and its implementation in the .NET Framework. This function may be convenient and familiar. But it simply invokes TrimStart.