Microsoft & .NETVisual BasicVB Coding Tip: Trim All Spaces From a String

VB Coding Tip: Trim All Spaces From a String

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Function TrimSpaces(text As String)

Dim trimspace As Integer
Dim thechar$, thechars$

If InStr(text, " ") = 0 Then
  TrimSpaces = text
  Exit Function
End If

For trimspace = 1 To Len(text)

thechar$ = Mid(text, trimspace, 1)
thechars$ = thechars$ & thechar$

If thechar$ = " " Then
  thechars$ = Mid(thechars$, 1, _
                  Len(thechars$) - 1)
End If

Next trimspace

TrimSpaces = thechars$

End Function
Private Sub cmdTrimSpaces_Click()
Text1=TrimSpaces(Text1)
End Sub

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories