[Solved] Is there an embedded opposite of strings.Repeat(“0”, 3) (detecting number of lead zeros) in Golang?


func LeadZeros(num string) int {

    i := 0
    for ;i < len(num) && num[i] == '0'; i++ {
    }

    return i
}

There are a lot of ways to do this and I would suggest the easiest to understand. Here is another one-liner:

func LeadZeros(num string) int {
    return len(num) - len(strings.TrimLeft(num, "0"))
}

1

solved Is there an embedded opposite of strings.Repeat(“0”, 3) (detecting number of lead zeros) in Golang?