Jump to content

Recommended Posts

Posted

Hello!

Can someone point me in the right direction? :D

I need to trim the leading zero's off of a numeric string.

For example:

I'm pulling the following sample information from a log file: 0000982 and would like to display it as 982.

Yes, I have read the Help File, especially the "string" and "Stringtrim" fuctions for help, but the problem is that there is not always 4 leading zero's. I was thinking I would have to evaluate each number starting from the left and delete it if it was equal to zero.

I just need to know where to start and I should be able to take it from there.

Thanks for any help! :oops:

Posted (edited)

Try this:

$string = "00000012345054321"
$new = StringReplace($string, StringRegExpReplace($string, "[^0].*", ""), "")
MsgBox(0, "Test", $new)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

  • Moderators
Posted

HockeyFan,

This seems to work quite well: :oops:

Global $aList[3] = [0000982, 00009082, 9082]

For $i = 0 To UBound($aList) - 1
    $sRet = StringRegExpReplace($aList[$i], "^0*(d+)", "$1")
    ConsoleWrite($sRet & @CRLF)
Next

SRE explanation:

^0*     - Any number of zeros starting at the beginning
(d+)   - Capture any digits following - obviously starting from anything other than a zero

Does it work for you? :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted (edited)

Another way is using Number().

MsgBox(0, "", Number("0000982"))
Edited by guinness

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

M23,

Thank you...this also works very well for me. :oops:

As with UEZ's posting, I am a bit confused about the the "^0*(d+)" paramater. Thank you so much for explaining this!!!!! :D

Posted

guinness,

I looked at the Number() function, but didnt think it applied to this situation. I should have at least tried it. Thanks for bringing it to my attention! :D

  • Moderators
Posted

HockeyFan,

I think we all did. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

  • 12 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...