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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Information is data with context. The era of Big Data has begun demonstrating to information security that there is more that can, and must, be done to identify threats, reduce risk, address fraud, and improve compliance monitoring activities by bringing better context to data and thereby creating information for actionable intelligence. This analyst report sets the stage and provides insights into IT and information security practitioners' perceptions of the impediments to, and the solutions necessary for, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds