Link to home
Start Free TrialLog in
Avatar of kay soo
kay soo

asked on

Count Leading Zero of String

Hi, can anyone show me how to count leading zero of a string?
I have searched the KB but not exactly what I am looking for, just want a simple count result of the number of leading Zero of a string.
Strings randomly consist of min 5 characters or max of 15 characters, all numeric, no alphabet involved.

e.g.
04421000
Result = 1

001540045456001
Result = 2

000123450100124
Result: = 3

121001421000041
Result = 0

Thx
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

try a function like this:

Function getLeadingZeroCount(ByVal v As String) As Integer
    tmp = Trim(v)
    If tmp = "" Then
        getLeadingZeroCount = 0
    Else
        For i = 1 To Len(tmp)
            If Mid(tmp, i, 1) = "0" Then
                getLeadingZeroCount = getLeadingZeroCount + 1
            Else
                Exit Function
            End If
        Next i
    End If
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@mbizup,

yup, a smart one
Avatar of kay soo
kay soo

ASKER

Thx for the solutions fellas, I go for @mbizup cause I used an unbound field to keep the count.