慕尼黑5688855
ftr,少正则表达式的解决方案package mainimport ( "fmt")func main() { inputs := []string{"Hallمرحباey's Comet", "First In, First Out", "The Road _Not_ Taken", "O'Brian's Dog"} c := [][]string{} w := [][]string{} for _, input := range inputs { c = append(c, firstLet(input)) w = append(w, words(input)) } fmt.Printf("%#v\n", w) fmt.Printf("%#v\n", c)}func firstLet(in string) (out []string) { var inword bool for _, r := range in { if !inword { if isChar(r) { inword = true out = append(out, string(r)) } } else if r == ' ' { inword = false } } return out}func words(in string) (out []string) { var inword bool var w []rune for _, r := range in { if !inword { if isChar(r) { w = append(w, r) inword = true } } else if r == ' ' { if len(w) > 0 { out = append(out, string(w)) w = w[:0] } inword = false } else if r != '_' { w = append(w, r) } } if len(w) > 0 { out = append(out, string(w)) } return out}func isChar(r rune) bool { return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')}输出[][]string{[]string{"Hallمرحباey's", "Comet"}, []string{"First", "In,", "First", "Out"}, []string{"The", "Road", "Not", "Taken"}, []string{"O'Brian's", "Dog"}}[][]string{[]string{"H", "C"}, []string{"F", "I", "F", "O"}, []string{"T", "R", "N", "T"}, []string{"O", "D"}}