[Golang] Count Leading Spaces of a String
Count leading spaces of a string in Golang.
package main
import "fmt"
func countLeadingSpace(line string) int {
i := 0
for _, runeValue := range line {
if runeValue == ' ' {
i++
} else {
break
}
}
return i
}
func main() {
fmt.Println(countLeadingSpace("0 space"))
fmt.Println(countLeadingSpace(" 1 space"))
fmt.Println(countLeadingSpace(" 2 space"))
fmt.Println(countLeadingSpace(" 3 space"))
}
Tested on: Ubuntu Linux 15.10, Go 1.5.3.
References:
[1] | golang leading space |