Count Leading Zero of String

kay soo
kay soo used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
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

Nerd
Most Valuable Expert 2012
Top Expert 2013
Commented:
If you're just looking for a count of leading zeros, compare the length of the whole string to the length of the value of the string (leading zeros dropped):

msgbox "Number of Leading Zeros: " & len(s) - Len(cstr(val(s)))

Open in new window

Ryan ChongSoftware Team Lead

Commented:
@mbizup,

yup, a smart one

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial