trim leading zeroes AND last 2 characters

B

Barbara Brenner

Someone from this group posted a solution -- a function -- for trimming
leading zeroes. This was awhile back (1999). I just tried it and it worked
great:

Public Function StripLeadZeroes(Txt As String) As String

Dim Lgth As Integer
Dim Cnt As Integer
Dim Char As String


Txt = Nz(Txt,"") ' make sure we aren't dealing with a null value
Lgth = Len(Txt) ' get length of text string passed
If Lgth > 1 Then ' check if string is more than 1 char - if only 1 char
then there is nothing to strip

For Cnt = 1 To Lgth

Char = Mid(Txt, Cnt, 1)

If Char = "0" Then
Mid(Txt, Cnt, 1) = " "
Else
Exit For
End If

Next Cnt

End If

StripLeadZeroes = Trim(Txt)


End Function

----------------------------------------------------------------------------
------------------

But now I need to incorporate an additional process--I need to get rid of
the last 2 characters of the string as well.

Can you help?

BB
 
J

Jeff Boyce

Barbara

While the function you posted looks like it will strip off leading zeros in
a text string, it also looks like it will "alter" any 0 it finds within a
set of contiguous digits (e.g., 10101), resulting in a string that doesn't
look much like the original (e.g., "00010101" becomes "1 1 1"). Is that
what you need?

Good luck

Jeff Boyce
<Access MVP>
 
B

Barbara Brenner

It didn't seem to interfere with anything in that manner. I just checked a
number that was 000103 and is now 103.

Thanks,

Barbara
 
B

Barbara Brenner

Thanks, Fred! You helped me out a few years ago when I had to convert to
caps and strip out all but ASCII characters to sort the way I needed to.

Barbara

Fredg said:
To remove the last 2 characters:

YourString = Left([YourString],len([YourString])-2)

--
Fred

Please reply only to this newsgroup.
I do not reply to personal e-mail.


Barbara Brenner said:
Someone from this group posted a solution -- a function -- for trimming
leading zeroes. This was awhile back (1999). I just tried it and it worked
great:

Public Function StripLeadZeroes(Txt As String) As String

Dim Lgth As Integer
Dim Cnt As Integer
Dim Char As String


Txt = Nz(Txt,"") ' make sure we aren't dealing with a null value
Lgth = Len(Txt) ' get length of text string passed
If Lgth > 1 Then ' check if string is more than 1 char - if only 1 char
then there is nothing to strip

For Cnt = 1 To Lgth

Char = Mid(Txt, Cnt, 1)

If Char = "0" Then
Mid(Txt, Cnt, 1) = " "
Else
Exit For
End If

Next Cnt

End If

StripLeadZeroes = Trim(Txt)


End Function

--------------------------------------------------------------------------
--
------------------

But now I need to incorporate an additional process--I need to get rid of
the last 2 characters of the string as well.

Can you help?

BB
 
J

Jeff Boyce

<V8 moment>

Sorry, Barbara, I missed the Exit For entirely. The function worked fine
here, too.

My bad!

Jeff Boyce
<Access MVP>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top