strings Package Functions
Split, join, and trim.
strings Package Functions is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The strings Package
The standard library strings package gives you ready-made helpers for working with text: searching, splitting, joining, trimming, and changing case.
Import it with import "strings".
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ToUpper("hello"))
}Contains and HasPrefix
Check for substrings without writing loops:
strings.Contains(s, sub)reports ifsubappears.strings.HasPrefixandstrings.HasSuffixcheck the start and end.
package main
import (
"fmt"
"strings"
)
func main() {
s := "golang"
fmt.Println(strings.Contains(s, "lan"))
fmt.Println(strings.HasPrefix(s, "go"))
}Finding a Position
strings.Index(s, sub) returns the byte index of the first match, or -1 if not found.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Index("chocolate", "col"))
fmt.Println(strings.Index("chocolate", "xyz"))
}Splitting a String
strings.Split(s, sep) breaks a string into a slice using a separator.
It is perfect for parsing CSV-style data.
package main
import (
"fmt"
"strings"
)
func main() {
parts := strings.Split("a,b,c", ",")
fmt.Println(parts)
fmt.Println(len(parts))
}Splitting by Whitespace
strings.Fields(s) splits around any run of whitespace and drops empty pieces. It is ideal for word counting.
package main
import (
"fmt"
"strings"
)
func main() {
words := strings.Fields(" the quick brown fox ")
fmt.Println(words)
fmt.Println(len(words))
}Joining Slices
strings.Join(slice, sep) is the inverse of Split: it glues slice elements together with a separator.
package main
import (
"fmt"
"strings"
)
func main() {
parts := []string{"2026", "05", "30"}
fmt.Println(strings.Join(parts, "-"))
}Trimming Spaces
strings.TrimSpace(s) removes leading and trailing whitespace, which is great for cleaning user input.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.TrimSpace(" hi there ") + "!")
}Trimming Specific Characters
strings.Trim(s, cutset) removes any of the given characters from both ends. TrimPrefix and TrimSuffix remove exact strings.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Trim("***name***", "*"))
fmt.Println(strings.TrimSuffix("file.txt", ".txt"))
}Replacing Text
strings.ReplaceAll(s, old, new) swaps every occurrence. Use strings.Replace(s, old, new, n) to limit to the first n matches.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ReplaceAll("a-b-c", "-", "+"))
fmt.Println(strings.Replace("a-b-c", "-", "+", 1))
}Repeat and Count
Two more handy helpers:
strings.Repeat(s, n)builds a repeated string.strings.Count(s, sub)counts non-overlapping occurrences.
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Repeat("ab", 3))
fmt.Println(strings.Count("banana", "a"))
}Combining Functions
These helpers compose well. Here we trim, split, and rejoin a messy string into a clean comma list.
package main
import (
"fmt"
"strings"
)
func main() {
raw := " go rust zig "
words := strings.Fields(raw)
fmt.Println(strings.Join(words, ", "))
}Quick Check
Pick the function that joins a slice into one string.
Recap: strings Functions
You now have a toolkit for text:
- Search:
Contains,Index,HasPrefix. - Break apart:
Split,Fields. - Combine:
Join,Repeat. - Clean:
TrimSpace,Trim,ReplaceAll.
package main
import (
"fmt"
"strings"
)
func main() {
s := " Hello, Go World "
fmt.Println(strings.ToLower(strings.TrimSpace(s)))
}Frequently asked questions
Is the “strings Package Functions” lesson free?
Yes — the full text of “strings Package Functions” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “strings Package Functions”?
Split, join, and trim. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “strings Package Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Runes vs Bytes
- strings Package Functions
- strings.Builder
- Unicode and utf8