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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • VMware vCloud® Government Service provided by Carpathia® is an enterprise-class hybrid cloud service that delivers the tried and tested VMware capabilities widely used by government organizations today, with the added security and compliance assurance of FedRAMP authorization. The hybrid cloud is becoming more and more prevalent – in fact, nearly three-fourths of large enterprises expect to have hybrid deployments by 2015, according to a recent Gartner analyst report. Learn about the benefits of …

Most Popular Programming Stories

More for Developers

RSS Feeds