Strings Package
Fast recall
Quick revision
Core answer
Whether you understand Strings Package beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The kit, grouped by job (all executed):
// searching
strings.Contains("hello world", "wor") // true
strings.HasPrefix("main.go", "main") // true
strings.Index("hello", "l") // 2 — byte offset, -1 if absent
// splitting & joining
strings.Split("a,b,c", ",") // [a b c]
strings.Join([]string{"x","y"}, "-") // x-y
strings.Fields(" a b c ") // [a b c] — any whitespace
// case & trimming
strings.ToUpper("go") // GO
strings.TrimSpace(" hi \n") // hi — both ends, all whitespace
// replacing & building
strings.Replace("a-b-c", "-", "+", 2) // a+b+c (count; -1 = all)
strings.Repeat("go", 3) // gogogo
var b strings.Builder
b.WriteString("hello ")
b.WriteString("world")
b.String() // "hello world" — the loop-concat fixThe design notes that make this the answer rather than a list: Index returns BYTE offsets — composable with slicing (s[:idx]). Fields handles messy whitespace (the parsing case); Split is for a known delimiter. The rune-awareness: ToUpper/ToLower/Trim operate on RUNES (unicode-correct), while Index/Contains match raw byte substrings (fast, exact).
Two functions people forget and reinvent: Fields (the whitespace splitter) and Builder (the O(n) concatenator).
Strings Package
Direct Answer
Contains, HasPrefix/HasSuffix, Index, Split/Join, ToUpper/ToLower, TrimSpace, Replace, Fields, Repeat, Builder. Searching, splitting, case/trim, replacing, building — the everyday text toolkit, Unicode-correct where it matters.
Deep Dive & Architecture
The function taxonomy by implementation cost: Index/Contains/HasPrefix are byte-wise substring searches (fast — the runtime uses optimized search); Split/Fields/Replace allocate (the results are new); TrimSpace/ToUpper/ToLower decode runes (Unicode-correct — the case tables come from the unicode package); Join/Repeat size their output exactly (one allocation).
The rune-vs-byte split inside one package: ToUpper/ToLower use unicode case tables — multibyte runes change correctly (é→É). Trim/TrimLeft/TrimRight accept a CUTSET (any of these runes — not a prefix!). Index/Contains match raw bytes — a pattern that happens to be invalid UTF-8 can never match inside a valid UTF-8 string (encoding boundaries align), which is why byte-search is safe for text.
The mistakes each function exists to prevent: manual whitespace loops (Fields), manual comma-joining (Join), += concatenation (Builder), manual ASCII case switching (ToUpper works on the whole range), the index-of+slicing dance for prefixes (HasPrefix reads as intent).
The missing-from-strings worth knowing: unicode/utf8 (RuneCountInString, DecodeRuneInString — the rune machinery), strings.Cut (Go 1.18 — split-once, the parsing workhorse), strings.EqualFold (case-insensitive compare), strings.Clone (the substring-anchor-breaker). Name the function for the job and the one-liner it replaced.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Strings Package in a real project?
- How would you compare Strings Package with an alternative?
- What is a common mistake when using Strings Package?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.