WelcomeServicesPortfolioKnowledge BaseContact Us
27 Seconds, Inc.


Knowledge base View Article

need hosting?

Need hosting? We use and love our host - CrystalTech.com!
Need hosting? We use and love our host - CrystalTech.com!

Text Disguise - CAPTCHA-image/Human Interactive Proof web services

National Holiday Dates and Bank Holiday Dates - web site that provides the dates of national and bank holidays for the United States, United Kingdom, Ireland and Scotland

Holiday Web Service - web services for programmers to provide the dates of national and bank holidays in their applications for the United States, United Kingdom, Ireland and Scotland

Our Family Heart - web site to help families communicate no matter where they are

What?! You haven't heard about the greatest remote controlled flyer to come out in years?! Checkout the FlyTech DragonFly now!

Remove characters from beginning and end of string.

Summary: Going beyond a simple trim -- this function removes CR/LF and tabs from the beginning and ending of a string.
 
Keywords: BAD
CHARACTERS
REMOVE
TRIM
Applicable Software: Active Server Pages (ASP)
 
Body:

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" )

 
Author: Douglas L. Setzer, II, http://www.27seconds.com
Posted On: 3/5/2002 12:12:13 PM

Rate this article: Average: 1
n/a12345678910
Comments?

Article Search   |   All Articles

 

 
©2002 27 Seconds, Inc. All Rights Reserved.