Sometimes you want to make sure that there is no white-space at the beginning of a string, at all. Not carraige returns/line feeds, tabs, spaces, etc.
There are now 2 versions of the same code. Version 2 is a much short version of #1. Credit goes out to Bill Wilkinson of Microsoft for version #2.
Version 1:
Dim aBAD_CHARS: aBAD_CHARS = Array(vbCr, vbLf, vbTab, " ")
Function removeBadChars(ByVal pstrSource)
Dim m_sOut
m_sOut = pstrSource
Dim m_bCleanString
m_bCleanString = False
Dim m_iCnt
Dim m_bBadCharFound
'check for CR/LF combinations at the beginning:
While Not m_bCleanString
m_bBadCharFound = False
For m_iCnt = 0 To UBound(aBAD_CHARS, 1)
If Left(m_sOut, Len(aBAD_CHARS(m_iCnt))) = aBAD_CHARS(m_iCnt) Then
m_sOut = Right(m_sOut, Len(m_sOut) - Len(aBAD_CHARS(m_iCnt)))
m_bBadCharFound = True
End If
If Right(m_sOut, Len(aBAD_CHARS(m_iCnt))) = aBAD_CHARS(m_iCnt) Then
m_sOut = Left(m_sOut, Len(m_sOut) - Len(aBAD_CHARS(m_iCnt)))
m_bBadCharFound = True
End If
Next
m_bCleanString = Not m_bBadCharFound
Wend
removeBadChars = m_sOut
End Function
And, to use it, you would do:
pstrSource = vbCrLf & vbCrLf & " " & vbTab & "The cat ate the: " & vbCrLf & "- dog. "
Response.Write "<pre>" & removeBadChars(pstrSource) & "</pre>"
Version 2:
Function RemoveTrailing( str, bad )
Do Until InStr( bad, Right(str, 1) ) = 0
str = Left( str, Len(str) - 1 )
Loop
RemoveTrailing = str
End Function
Dim whitespace
whitepace = vbCR & vbLF & vbTab & " "
Function RemoveTrailingWhitespace( str )
RemoveTrailingWhitespace = RemoveTrailing( str, whitespace )
End Function
And, to use it, you would do:
pstrSource = vbCrLf & vbCrLf & " " & vbTab & "The cat ate the: " & vbCrLf & "- dog. "
Response.Write "<pre>" & RemoveTrailingWhitespace(pstrSource) & "</pre>"
And, the nice part about version 2, is that you can call the worker function directly:
Response.Write RemoveTrailing( "this is a test 123", "321" )
|