Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove leading zeroes from a string

Tags:

vbscript

I have to extract the integer value from a string.
Its actually an amount field.

Say string can be 000000000000512 or 0000040000000
I want only the integer value from this string i.e.; 512/ 40000000
Please help with this in VB scripting

like image 671
user3350742 Avatar asked Feb 25 '14 10:02

user3350742


People also ask

How do you remove leading zeros from a string in Python?

The lstrip() method to remove leading zeros When used, it automatically removes leading zeros ( only ) from the string. Note that this works for numbers and all characters accepted as a string. However, another method strip() will remove the leading and ending characters from the string. Python lstrip() docs.

How do you remove leading zeros from a string in C++?

Using Stoi() Method stoi() function in C++ is used to convert the given string into an integer value. It takes a string as an argument and returns its value in integer form. We can simply use this method to convert our string to an integer value which will remove the leading zeros.

How do you remove leading zeros from a string in Java?

Given a string of digits, remove leading zeros from it. 1) Count leading zeros. 2) Use StringBuffer replace function to remove characters equal to above count.


2 Answers

CInt("000000000000512")

See conversion functions: http://msdn.microsoft.com/en-us/library/s2dy91zy.aspx

Use Clng if you expect to have large numbers, as already pointed out in a comment:

Clng("000000004000512")

otherwise you'll have an overflow, as variant's subtype int is 16 bit in vbscript

like image 78
morepaolo Avatar answered Nov 04 '22 03:11

morepaolo


This will work even with a crazy long number

Function RemoveLeadingZeroes(ByVal str)
Dim tempStr
tempStr = str
While Left(tempStr,1) = "0" AND tempStr <> ""
    tempStr = Right(tempStr,Len(tempStr)-1)
Wend
RemoveLeadingZeroes = tempStr
End Function

strNewFileName = RemoveLeadingZeroes("0009283479283749823749872392384")
like image 31
Urielzen Avatar answered Nov 04 '22 03:11

Urielzen