CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jul 1999
    Posts
    111

    LTrim(string) and Chr(9)

    Having had little experience with VB I was a bit surprised to see that the Trim family of functions strip only spaces. I had expected Trim to remove tabs as well.

    My question: what is the best way to strip all leading white space from a string?

    Roger


  2. #2
    Join Date
    Aug 1999
    Location
    India-Delhi
    Posts
    106

    Re: LTrim(string) and Chr(9)

    Roger,
    use following function to get rid of all characters except those having ascii code from 33 to 126. I wrote this code long back to get rid of all such problems.

    private Function RemoveAll(thistext as string) as string
    Oldtext = thistext
    'This will find that first letter is either string or Number only
    for i = 1 to len(Oldtext)
    If Not (Asc(mid(Oldtext, i, 1)) >= 32 And Asc(mid(Oldtext, i, 1)) <= 126) then
    mid(Oldtext, i, 1) = Space(1)
    End If
    next i
    RemoveAll = LTrim(RTrim(Oldtext))
    End Function




    I hope this solves the problem.

    Santulan

  3. #3
    Join Date
    Sep 1999
    Location
    India
    Posts
    1

    Re: LTrim(string) and Chr(9)

    if u don't mind stripping of white spaces after the string u can use Trim(String) which will strip all leading white spaces including tabs.

    Hope it will solve ur purpose.

    Ramapriya

  4. #4

    Re: LTrim(string) and Chr(9)


  5. #5
    Join Date
    Jul 1999
    Posts
    111

    Re: LTrim(string) and Chr(9)

    No... you are wrong here. Trim and it's cousins, LTrim and RTrim, remove only the space character (Chr(32)). See the article:

    http://www.freevbcode.com/ShowCode.Asp?ID=104

    For more information.



  6. #6
    Join Date
    Jul 1999
    Posts
    111

    Re: LTrim(string) and Chr(9)

    Thanks... It's unfortunate though that the code contains a few errors. I am an experienced programmer and was able to spot and fix them quickly, but I am afraid the mistakes will cause novice programmers some grief.

    Anyway, thanks for the link!

    Roger



  7. #7

    Re: LTrim(string) and Chr(9)

    Hi:

    I would like to know what the errors are, since I am using the function in several applications and have not run into problems.

    Thanks,

    Charlie


  8. #8
    Join Date
    Jul 1999
    Posts
    111

    Re: LTrim(string) and Chr(9)

    Well, you must have either fixed the errors or have a different copy of the code. The errors are:

    1) In both For loops the keyword Next needs to be followed by the count variable 'lCtr.

    2) the line: sAns = Mid(sAns, lCtr) should be: sAns = Right$(sAns, lCtr)

    Here is the code that is at the end of the link you gave me:


    public Function TrimWithoutPrejudice _
    (byval InputString as string) as string

    Dim sAns as string
    Dim sWkg as string
    Dim sChar as string
    Dim lLen as Long
    Dim lCtr as Long

    sAns = InputString
    lLen = len(InputString)

    If lLen > 0 then
    'Ltrim
    for lCtr = 1 to lLen
    sChar = mid(sAns, lCtr, 1)
    If Asc(sChar) > 32 then Exit for
    next

    sAns = mid(sAns, lCtr)
    lLen = len(sAns)

    'Rtrim
    If lLen > 0 then
    for lCtr = lLen to 1 step -1
    sChar = mid(sAns, lCtr, 1)
    If Asc(sChar) > 32 then Exit for
    next
    End If
    sAns = Left$(sAns, lCtr)
    End If

    TrimWithoutPrejudice = sAns

    End Function





    I hope this answers your question.

    Roger



  9. #9

    Re: LTrim(string) and Chr(9)

    <<1) In both For loops the keyword Next needs to be followed by the count variable 'lCtr.>>

    Including lctr after next is optional.

    <<2) sAns = Mid(sAns, lCtr) should be: sAns = Right$(sAns, lCtr)>>

    No, the string won't be properly Ltrimmed if you make that change. Try this test


    Dim s as string
    s = TrimWithoutPrejudice(" " & vbTab & " test" & " " & vbTab)
    Debug.print s 'Original way, s = "test"; your way, s = " test"
    Debug.print len(s) 'original way, prints 4, your way prints 6






  10. #10
    Join Date
    Jul 1999
    Posts
    111

    Re: LTrim(string) and Chr(9)

    Gosh, you know I was looking at a couple of other was to write this type of function and I obiviously got confused, because I was definately wrong about Right$. I must not have even run the code... I thought I did, but as I say I was looking at a few different variations on the same theme. BTW - thanks for pointing out the the variable after Next is optional, I missed that one...

    Now I'm a bit confused about something else with this function... there doesn't seem to be an End If to close the IF statements in either of the For loops...

    For instance here is the first For loop:


    for lCtr = 1 to lLen
    sChar = mid(sAns, lCtr, 1)
    If Asc(sChar) > 32 then
    Exit for
    next





    I get the error message 'Next without For'. If I insert a line before Next with End If as it's contents, it seems to solve the problem... can you explain how you run the code without the 'End If'? I don't mean to be smart. I just want to make sure I understand.

    Thanks!


  11. #11

    Re: LTrim(string) and Chr(9)

    In the code I have, the if statement and exit for statements are on the same line:
    If Asc(sChar) > 32 then Exit for



    See http://www.freevbcode.com/ShowCode.Asp?ID=104


  12. #12
    Join Date
    Jul 1999
    Posts
    111

    Re: LTrim(string) and Chr(9)

    OIC... I was not aware that you could do away with the End If by placing the body on the same line. I am learning VB after spending several years using C, where there are no (or very few) restrictions as to where newline characters appear in the statement. This thread has been very informative for me as I have learned more than I had hoped for.

    Do you think you could recomend a good text to help me get up to speed with VB? I know the help files are a good place to start, but if you know of any good text for a reader who has previous programming experience using another language, I would be interested in giving it a look.

    Thanks!
    Roger



  13. #13

    Re: LTrim(string) and Chr(9)

    I know of no books about VB specifically designed for C programmers. As for books about VB in general, I haven't read any, but I've seen more than one person recommend the following:

    Programming Visual Basic 6
    Author: Francesco Balena
    Format: Paperback, 1400 pages
    ISBN: 0735605580
    Publisher: Microsoft Press
    Date Published: May 1999

    Good luck.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured