trim() function does not work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BlackMustard
    New Member
    • Aug 2007
    • 88

    trim() function does not work

    hi all,

    i am having problems using the Trim() function in a program written in VB6. i'm getting my strings from reading a textfile line by line, but regardless of the trim function a couple of extra spaces are always included in the variable.

    this is the code i use:
    [code=vb]
    Dim filepath As String
    Dim FileNo As Long
    Dim LineNo, i As Integer
    Dim LineText As String

    FileNo = FreeFile ' Get next available file number.

    cdb.ShowOpen
    filepath = cdb.FileName

    Open filepath For Input Access Read Shared As #FileNo
    Do Until EOF(FileNo) ' Repeat until end of file...
    Line Input #FileNo, LineText ' Read a line from the file.
    LineNo = LineNo + 1

    txtFileName.Tex t = Trim(LineText) ' This line still leaves a couple of spaces at the end of the string in txtFileName.Tex t

    DoEvents ' Allow Windows to handle other tasks.
    Loop
    Close #FileNo
    [/code]

    why doesn't the Trim() function remove all tracing spaces?
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    Last Chars may be Chr(0) and not a Space. if they are Chr(0) then trim cannot remove those spaces. Instead use Replace Function :

    [code=vb]
    txtFileName.Tex t = Replace(Trim(Li neText),Chr(0), "")
    [/code]

    Regards
    Veena

    Comment

    • BlackMustard
      New Member
      • Aug 2007
      • 88

      #3
      Originally posted by QVeen72
      Hi,

      Last Chars may be Chr(0) and not a Space. if they are Chr(0) then trim cannot remove those spaces. Instead use Replace Function :

      [code=vb]
      txtFileName.Tex t = Replace(Trim(Li neText),Chr(0), "")
      [/code]

      Regards
      Veena
      nope - didn't work. the strings are still trailed by spaces. could you please give me a simple function to split the string into a character array so i can check what characters they really are?

      EDIT:
      never mind - i took the last character, found out that it was chr(160) that i (also?) needed to replace, and when i did it worked. but the question remains... is there a better way to get the file extensions than using right(linetext, 4)?

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Originally posted by BlackMustard
        could you please give me a simple function to split the string into a character array so i can check what characters they really are?
        Hi,

        use this :

        [code=vb]
        Dim i As Integer
        For i = 1 to Len(LineText)
        Debug.Print Mid(LineText,i, 1) & " Ascii = " & Asc(Mid(LineTex t,i,1))
        Next
        [/code]

        Regards
        Veena

        Comment

        Working...