Table of Contents
Go Stdlib Wiki: package strings
1. Overview
The Go standard library package strings provides utilities for working with strings: checking prefixes/suffixes, searching, splitting/joining, trimming, replacing, case conversion, and more.
string /strɪŋ/ = a sequence of characters (chuỗi ký tự)
standard library (stdlib) /ˈstændərd ˈlaɪbreri/ = thư viện chuẩn
Note: Go strings are UTF-8, but many strings functions operate on bytes (not “user-perceived characters”).
import "strings"
2. Common function groups
2.1 Prefix / Suffix checks
strings.HasPrefix(s, prefix) bool — reports whether s begins with prefix
strings.HasSuffix(s, suffix) bool — reports whether s ends with suffix
prefix /ˈpriːfɪks/ = tiền tố (đầu chuỗi)
suffix /ˈsʌfɪks/ = hậu tố (cuối chuỗi)
strings.HasPrefix("golang", "go") // true strings.HasSuffix("golang", "ng") // true
2.2 Search
strings.Contains(s, substr) bool — checks if substr appears in s
strings.Index(s, substr) int — index of first occurrence, or -1
strings.LastIndex(s, substr) int — index of last occurrence, or -1
strings.Count(s, substr) int — number of non-overlapping instances of substr
contains /kənˈteɪnz/ = chứa
substring (substr) /ˈsʌbst.rɪŋ/ = chuỗi con
occurrence /əˈkɝː.ə.rəns/ = lần xuất hiện
non-overlapping /ˌnɒn ˌoʊvərˈlæpɪŋ/ = không chồng lấp
strings.Contains("hello world", "world") // true strings.Index("banana", "na") // 2 strings.LastIndex("banana", "na") // 4 strings.Count("banana", "na") // 2
2.3 Split / Join
strings.Split(s, sep) []string — splits s around each instance of sep
strings.SplitN(s, sep, n) []string — split into at most n parts
strings.Fields(s) []string — splits around consecutive whitespace
strings.Join(elems, sep) string — joins string slice with sep
split /splɪt/ = tách
separator (sep) /ˈsepəˌreɪtər/ = ký tự/chuỗi phân cách
fields /fiːldz/ = các “mảnh” tách theo khoảng trắng
join /dʒɔɪn/ = nối
strings.Split("a,b,c", ",") // ["a","b","c"] strings.SplitN("a,b,c", ",", 2) // ["a","b,c"] strings.Fields(" hi go \n ok ") // ["hi","go","ok"] strings.Join([]string{"a","b","c"}, "-") // "a-b-c"
2.4 Replace / Repeat
strings.Replace(s, old, new, n) string — replaces up to n matches (-1 = all)
strings.ReplaceAll(s, old, new) string — replaces all matches
strings.Repeat(s, count) string — repeats s count times
replace /rɪˈpleɪs/ = thay thế
repeat /rɪˈpiːt/ = lặp lại
strings.Replace("a a a", "a", "b", 2) // "b b a" strings.ReplaceAll("a a a", "a", "b") // "b b b" strings.Repeat("go", 3) // "gogogo"
2.5 Trim
strings.TrimSpace(s) string — trims leading/trailing whitespace
strings.Trim(s, cutset) string — trims any leading/trailing Unicode code points in cutset
strings.TrimPrefix(s, prefix) string — removes prefix if present
strings.TrimSuffix(s, suffix) string — removes suffix if present`
trim /trɪm/ = cắt bỏ
whitespace /ˈwaɪt.speɪs/ = khoảng trắng (space/tab/newline)
cutset /ˈkʌtˌset/ = tập ký tự cần cắt
strings.TrimSpace(" hello \n") // "hello" strings.Trim("...hello...", ".") // "hello" strings.TrimPrefix("https://a.com", "https://")// "a.com" strings.TrimSuffix("file.txt", ".txt") // "file"
2.6 Case conversion & case-insensitive compare
strings.ToLower(s) string — to lowercase
strings.ToUpper(s) string — to uppercase
strings.EqualFold(s, t) bool — case-insensitive equality (Unicode-aware)
lowercase /ˈloʊərˌkeɪs/ = chữ thường
uppercase /ˈʌpərˌkeɪs/ = chữ hoa
case-insensitive /ˌkeɪs ɪnˈsensɪtɪv/ = không phân biệt hoa thường
equality /ɪˈkwɒləti/ = sự bằng nhau
strings.ToLower("GoLang") // "golang" strings.ToUpper("GoLang") // "GOLANG" strings.EqualFold("Go", "gO") // true
2.7 Build strings efficiently (Builder)
strings.Builder — efficient incremental string construction
builder /ˈbɪldər/ = bộ xây dựng (dùng để nối chuỗi hiệu quả)
var b strings.Builder b.WriteString("hello") b.WriteByte(' ') b.WriteString("world") s := b.String() // "hello world"
Why use it?
Repeated + concatenation can create many intermediate strings.
Builder reduces allocations in loops.
concatenation /kənˌkætəˈneɪʃən/ = phép nối chuỗi
allocation /ˌæləˈkeɪʃən/ = cấp phát bộ nhớ
3. Practical mini-recipes
3.1 Check a URL scheme
func IsHTTPS(url string) bool { return strings.HasPrefix(url, "https://") }
3.2 Split CSV-like input safely
parts := strings.Split(strings.TrimSpace(input), ",")
3.3 Normalize user input
normalized := strings.ToLower(strings.TrimSpace(s))
4. Notes & pitfalls
strings.Index returns byte index.
For complex Unicode character handling, you may need unicode/utf8 or converting to []rune.
strings.Trim(s, cutset) trims any of those characters, not the whole substring.
rune /ruːn/ = ký tự Unicode (trong Go)
pitfall /ˈpɪt.fɔːl/ = “bẫy” thường gặp
