Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Most efficient way to remove leading zeros from character string.

988 views
Skip to first unread message

Ryan Gaudet

unread,
Feb 26, 2003, 6:46:16 PM2/26/03
to
HI,

I tried posting this about an hour and a half ago but it
hasn't shown up so I'm posting it again.

Anyways,

I'm hoping someone can tell me the most efficient way to
do the following;
I have a character string which will always be six
charaters long (ie: "015478"). I need to evaluate the
expression and remove any leading zeros from it and
rewrite it as a character expression. Sometimes there will
be more than on leading zero and at times, there may not
be a leading zero.
I have a strong FoxPro background but am somewhat
unfamiliar with VB syntax and am hoping someone could
suggest the best way to do this.

Thanks in advance.

Ryan Gaudet

Bob Butler

unread,
Feb 26, 2003, 6:58:18 PM2/26/03
to
"Ryan Gaudet" <rgau...@hotmail.com> wrote in message
news:074501c2ddf1$40cf27d0$8ef82ecf@TK2MSFTNGXA04

> HI,
>
> I tried posting this about an hour and a half ago but it
> hasn't shown up so I'm posting it again.
>
> Anyways,
>
> I'm hoping someone can tell me the most efficient way to
> do the following;
> I have a character string which will always be six
> charaters long (ie: "015478").

if you are sure it will always be numeric you can try: CStr(CLng("015478"))

Ryan Gaudet

unread,
Feb 26, 2003, 7:21:42 PM2/26/03
to
>.
>Thanks for the quick response;
Actually, here's a but more detail, I'm pulling a text
value from an ini file which will be six characters long
(ie: "012345"). This is the value that I want to strip the
leading zeros ultimately ending up with (in this example)
a final value of "12345". If I had an initial value
of "000234", I would want to end up with a final value
of "234"
Thanks.

GumDrop

unread,
Feb 26, 2003, 8:14:04 PM2/26/03
to
do while left(Mystring,1) = "0"
MyString = mid(Mystring,2)
Loop


"Ryan Gaudet" <rgau...@hotmail.com> wrote in message

news:074501c2ddf1$40cf27d0$8ef82ecf@TK2MSFTNGXA04...

Bob Butler

unread,
Feb 26, 2003, 8:27:45 PM2/26/03
to
"Ryan Gaudet" <rgau...@hotmail.com> wrote in message
news:08dd01c2ddf6$3455d990$2f01...@phx.gbl

>> -----Original Message-----
>> "Ryan Gaudet" <rgau...@hotmail.com> wrote in message
>> news:074501c2ddf1$40cf27d0$8ef82ecf@TK2MSFTNGXA04
<cut>

>> if you are sure it will always be numeric you can try:
CStr(CLng("015478"))
>> Thanks for the quick response;
> Actually, here's a but more detail, I'm pulling a text
> value from an ini file which will be six characters long
> (ie: "012345"). This is the value that I want to strip the
> leading zeros ultimately ending up with (in this example)
> a final value of "12345". If I had an initial value
> of "000234", I would want to end up with a final value
> of "234"

Did you try it?

Rick Rothstein

unread,
Feb 26, 2003, 9:29:27 PM2/26/03
to
"Bob Butler" <butl...@earthlink.net> wrote in message
news:uT7mzLf3...@TK2MSFTNGP12.phx.gbl...

I guess if it isn't all digits (hex numbers for instance) and if you
want to keep it as a one-liner, then this is a possibility...

Replace$(LTrim$(Replace$("015478", "0", " ")), " ", "0")

<bg>

Rick - MVP


Bob Butler

unread,
Feb 26, 2003, 10:48:28 PM2/26/03
to
"Rick Rothstein" <rickNOS...@NOSPAMcomcast.net> wrote in message
news:eOefTgg3...@TK2MSFTNGP12.phx.gbl
<cut>

> I guess if it isn't all digits (hex numbers for instance) and if you
> want to keep it as a one-liner, then this is a possibility...
>
> Replace$(LTrim$(Replace$("015478", "0", " ")), " ", "0")
>
> <bg>

You're a sick boy Rick! <g>
Maybe we need to start a 12-step program for people who want to condense
everything to 1 line. Or should that be a 1-step program?

Rick Rothstein

unread,
Feb 27, 2003, 12:10:19 AM2/27/03
to
> > I guess if it isn't all digits (hex numbers for instance) and if you
> > want to keep it as a one-liner, then this is a possibility...
> >
> > Replace$(LTrim$(Replace$("015478", "0", " ")), " ", "0")
> >
> > <bg>
>
> You're a sick boy Rick! <g>

I'm many, many, many years past the age where "boy" might apply. On the
other hand, I would find it difficult indeed to argue with the word
"sick" as you intended it to be read (although I will point out that the
code **does** work<g>).


> Maybe we need to start a 12-step program for people who want to
> condense everything to 1 line. Or should that be a 1-step program?

One-step, of course; but I don't think I will participate in the
program... too worried about the withdrawal symptoms. <g>

Rick - MVP


Jan Hyde

unread,
Feb 27, 2003, 4:18:54 AM2/27/03
to
"Ryan Gaudet" <rgau...@hotmail.com>'s wild thoughts were
released on Wed, 26 Feb 2003 15:46:16 -0800 bearing the
following fruit:


? format$("00000015478","#")

J

--

The past does not repeat itself, but it rhymes.

Mark Twain

Ryan Gaudet

unread,
Feb 27, 2003, 8:56:55 PM2/27/03
to
Thanks everyone for your responses.

What I've ended up doing is simply taking the string with
the zeros (ie:"003456"), converting it into a numeric
value which removes the insignificant zeros automatically,
and then convert it back into a text string trimming any
spaces.
Although it may not be the "most efficient" or "right" way
to do it, I've tested it enough for the application I'll
be using it in.

Thanks again everyone and I have noted everyone's
suggestions in case my solution doesn't work out.

Ryan

>.
>

be...@vips.com

unread,
Jan 25, 2005, 3:38:22 PM1/25/05
to
I know this is an old thread, but I just wanted to say thanks. This
was a great handy little way to do this.

Ben

0 new messages