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

28 lines
528 B
Go
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
package main
import (
2026-04-30 12:34:36 -04:00
"fmt"
"strings"
"unicode"
2023-07-01 11:58:00 -04:00
)
const commentChars = "#;"
func stripComment(source string) string {
2026-04-30 12:34:36 -04:00
if cut := strings.IndexAny(source, commentChars); cut >= 0 {
return strings.TrimRightFunc(source[:cut], unicode.IsSpace)
}
return source
2023-07-01 11:58:00 -04:00
}
func main() {
2026-04-30 12:34:36 -04: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))
}
2023-07-01 11:58:00 -04:00
}