Click to See Complete Forum and Search --> : LTrim(string) and Chr(9)
Roger C
September 21st, 1999, 03:11 PM
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
santulan
September 22nd, 1999, 12:18 AM
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
Rama Priya
September 22nd, 1999, 09:44 AM
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
czimmerman
September 23rd, 1999, 12:27 AM
See http://www.freevbcode.com/ShowCode.Asp?ID=104
Roger C
September 23rd, 1999, 09:36 AM
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.
Roger C
September 23rd, 1999, 10:01 AM
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
czimmerman
September 23rd, 1999, 05:54 PM
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
Roger C
September 24th, 1999, 09:26 AM
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
czimmerman
September 24th, 1999, 04:51 PM
<<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
Roger C
September 27th, 1999, 09:27 AM
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!
czimmerman
September 27th, 1999, 11:37 AM
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
Roger C
September 28th, 1999, 10:33 AM
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
czimmerman
September 28th, 1999, 04:59 PM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.