
8

[Golang] Count Leading Spaces of a String
source link: http://siongui.github.io/2016/02/05/go-count-leading-space-of-string/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Golang] Count Leading Spaces of a String
February 05, 2016
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:
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK