RosettaCodeData/Task/Strip-comments-from-a-string/Go/strip-comments-from-a-string.go

28 lines
486 B
Go
Raw Permalink Normal View History

2013-04-11 01:07:29 -07:00
package main
import (
2015-02-20 00:35:01 -05:00
"fmt"
"strings"
"unicode"
2013-04-11 01:07:29 -07:00
)
const commentChars = "#;"
func stripComment(source string) string {
2015-02-20 00:35:01 -05:00
if cut := strings.IndexAny(source, commentChars); cut >= 0 {
return strings.TrimRightFunc(source[:cut], unicode.IsSpace)
}
return source
2013-04-11 01:07:29 -07:00
}
func main() {
2015-02-20 00:35:01 -05:00
for _, s := range []string{
"apples, pears # and bananas",
"apples, pears ; and bananas",
"no bananas",
} {
fmt.Printf("source: %q\n", s)
fmt.Printf("stripped: %q\n", stripComment(s))
}
2013-04-11 01:07:29 -07:00
}