Today Iâ€™ve been continuing with a Visual Basic project at work, and I ran into an issue whereby after I convert an Integer to a String using the Str(strMyString) command, the resulting string was given a leading space. This is a known outcome from the use of the Str command however I needed to find a solution.
I today discovered the LTrim and RTrim commands.
Using the LTrim command, declared as a second string you are able to produce the following:
Dim strMyString As String Dim strMyStringLeft As String strMyString = " Some Text " strMyStringLeft = LTrim (strMyString)
As you can see, my string starts life as â€œ Some Text â€œ. Once passed through the LTrim grinder, the value of strMyStringLeft will be â€œSome Text â€œ. As you can guess, passing strMyStringLeft through another variable will remove the leading whitespace as follows:
Dim strMyString As String Dim strMyStringLeft As String Dim strMyStringRight As String strMyString = " Some Text " strMyStringLeft = LTrim(strMyString) strMyStringRight = RTrim(strMyStringLeft)
The value of strMyStringRight would now be â€œSome Textâ€. This is achieved because we used strMyStringLeft as the input for strMyStringRight meaning that the leading space was already removed.
Although my way of doing this is probably a little longer than some VB guru will do by combing the statements, itâ€™s easy for a beginner like me to interpret the code.