The strings package in Go
Trimming, splitting, joining, and building text.
func textOps(input string) (string, int, []string) {
trimmed := strings.TrimSpace(input)
upper := strings.ToUpper(trimmed)
count := strings.Count(trimmed, "o")
parts := strings.Split(trimmed, " ")
var b strings.Builder
for _, part := range parts {
b.WriteString(strings.Title(part))
}
joined := strings.Join(parts, "-")
_ = upper
return joined + b.String(), count, parts
}
How it works
TrimSpace,ToUpper, andCountinspect text.SplitandJoinconvert to and from slices.strings.Builderconcatenates efficiently.
Keywords and builtins used here
forfuncintrangereturnstringvar
The run, in numbers
- Lines
- 14
- Characters to type
- 373
- Tokens
- 103
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 65 seconds.
Step 1 of 5 in Strings & formatting, step 25 of 30 in Language basics.