Improved Trim$ Statement

The Visual Basic Trim$() function has a serious shortcoming in that it only handles space characters and not all of the usual white spaces such as tabs, carriage returns, and line feeds.

The following functions is interchangeable with the standard Trim , but will handle all of the white spaces. In fact, if you look closely, you will discover that it can be configured to trim off any character that you would like.



public Function TrimAll(ToTrim as string) as string

    Dim Start, Finish as Integer
    Dim ToEliminate as string

    ' Base condition test
    If len(ToTrim) = 0 then
        TrimAll = ""
        Exit Function
    End If

    ' Define the characters that we want to trim off
    ToEliminate = Chr(0) & Chr(8) & Chr(9) & Chr(10) & _
                  Chr(13) & Chr(32)

    ' Find the beginning of non-blank string
    Start = 1
    While InStr(1, ToEliminate, mid$(ToTrim, Start, 1), _
                vbTextCompare) <> 0 And Start <= len(ToTrim)
        Start = Start + 1
    Wend

    ' Find the end of non-blank string
    Finish = len(ToTrim)
    While InStr(1, ToEliminate, mid$(ToTrim, Finish, 1), _
                vbTextCompare) <> 0 And Finish > 1
        Finish = Finish - 1
    Wend
    '
    ' If the string is completely blank, Start is going to be greater 
    ' than Finish
    '
    If Start > Finish then
        TrimAll = ""
        Exit Function
    End If

    ' Trim out the real contents
    TrimAll = mid$(ToTrim, Start, Finish - Start + 1)

End Function


Comments

  • Thnk you very much that;s what i want

    Posted by Legacy on 08/27/2003 12:00am

    Originally posted by: Isam Ahmed Hassan

    From my deep heart thank you very much
    

    Reply
  • A TrimAll function with very little code

    Posted by Legacy on 07/15/2003 12:00am

    Originally posted by: Brian Gillham

    Public Function TrimAll(TextIn, Optional TrimChar = " ") As String
    
    

    On Error GoTo LocalError

    TrimChar = CStr(TrimChar)
    TrimAll = CStr(TextIn)
    TrimAll = Replace(TrimAll, TrimChar, vbNullChar)
    While InStr(TrimAll, String(2, vbNullChar)) > 0
    TrimAll = Replace(TrimAll, String(2, vbNullChar), vbNullChar)
    Wend

    ' Delete Leading and Trailing
    If Left(TrimAll, 1) = vbNullChar Then TrimAll = Right(TrimAll, Len(TrimAll) - 1)
    If Right(TrimAll, 1) = vbNullChar Then TrimAll = Left(TrimAll, Len(TrimAll) - 1)

    LocalError:
    TrimAll = Replace(TrimAll, vbNullChar, TrimChar, , , vbTextCompare)
    End Function

    Reply
  • reference for Trim

    Posted by Legacy on 05/15/2003 12:00am

    Originally posted by: Maria

    I try to use one of your example but i don�t know which is the reference or the component for the function Trim. Could any one help me?
    
    Thank you,
    Maria

    Reply
  • It is what I want

    Posted by Legacy on 12/03/2002 12:00am

    Originally posted by: Maggie

    I can use it to trim the front and end blank
    characters when try to parse the commandline argument.

    Reply
  • just what i need!

    Posted by Legacy on 09/23/2002 12:00am

    Originally posted by: kiat

    thanks so much, exactly the neat little function that is truely useful.

    Reply
  • Thank Alot

    Posted by Legacy on 08/21/2002 12:00am

    Originally posted by: Witty Chip

    Can you help me ?
    I need "notepad" source code

    Reply
  • Logical Search

    Posted by Legacy on 06/18/2002 12:00am

    Originally posted by: Rodrigo Silva

    Those logical searches somehow are not so logical to me. You really helped!

    Reply
  • Thank you!

    Posted by Legacy on 12/27/2001 12:00am

    Originally posted by: Scott Whigham

    Oh this is sooo helpful! I have been trying to figure out a problem for a few hours that was driving me crazxy (SQL uploads and ASP). Turns out that this function solved everything.

    Thank you!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

  • On-demand Event Event Date: August 14, 2014 Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today makes data protection a must-have, as we live in a data-driven society -- the digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this webcast and join Seagate Cloud Systems and Solutions Chief …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds