package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println("=== 字符串查找 ===")
str := "Hello, World! Hello, Go!"
fmt.Printf("Contains 'World': %v\n", strings.Contains(str, "World"))
fmt.Printf("Contains 'world': %v\n", strings.Contains(str, "world"))
fmt.Printf("ContainsAny '123!': %v\n", strings.ContainsAny(str, "123!"))
fmt.Printf("Count 'Hello': %v\n", strings.Count(str, "Hello"))
fmt.Printf("Index 'World': %v\n", strings.Index(str, "World"))
fmt.Printf("Index 'Python': %v\n", strings.Index(str, "Python"))
fmt.Printf("LastIndex 'Hello': %v\n", strings.LastIndex(str, "Hello"))
fmt.Println("\n=== 字符串转换 ===")
fmt.Printf("ToUpper: %v\n", strings.ToUpper(str))
fmt.Printf("ToLower: %v\n", strings.ToLower(str))
fmt.Printf("Title: %v\n", strings.Title("hello world"))
fmt.Println("\n=== 字符串修剪 ===")
spacedStr := " Hello, World! "
fmt.Printf("Trim spaces: '%v'\n", strings.TrimSpace(spacedStr))
fmt.Printf("TrimLeft spaces: '%v'\n", strings.TrimLeft(spacedStr, " "))
fmt.Printf("TrimRight spaces: '%v'\n", strings.TrimRight(spacedStr, " "))
str2 := "test.txt"
fmt.Printf("TrimSuffix: '%v'\n", strings.TrimSuffix(str2, ".txt"))
fmt.Println("\n=== 字符串分割和连接 ===")
parts := strings.Split("a,b,c", ",")
fmt.Printf("Split: %v\n", parts)
partsN := strings.SplitN("a,b,c,d", ",", 2)
fmt.Printf("SplitN: %v\n", partsN)
joined := strings.Join([]string{"Hello", "World"}, " ")
fmt.Printf("Join: %v\n", joined)
fmt.Println("\n=== 字符串替换 ===")
replaced := strings.Replace("hello hello hello", "hello", "hi", 2)
fmt.Printf("Replace: %v\n", replaced)
replacedAll := strings.ReplaceAll("hello hello hello", "hello", "hi")
fmt.Printf("ReplaceAll: %v\n", replacedAll)
fmt.Println("\n=== 字符串比较 ===")
fmt.Printf("Compare 'a' and 'b': %v\n", strings.Compare("a", "b"))
fmt.Printf("Compare 'b' and 'a': %v\n", strings.Compare("b", "a"))
fmt.Printf("Compare 'a' and 'a': %v\n", strings.Compare("a", "a"))
fmt.Printf("EqualFold: %v\n", strings.EqualFold("Hello", "hello"))
fmt.Println("\n=== 字符串检查 ===")
fmt.Printf("HasPrefix: %v\n", strings.HasPrefix("Hello, World", "Hello"))
fmt.Printf("HasSuffix: %v\n", strings.HasSuffix("Hello, World", "World"))
fmt.Println("\n=== 字符串重复 ===")
repeated := strings.Repeat("Go! ", 3)
fmt.Printf("Repeat: %v\n", repeated)
fmt.Println("\n=== 字符串映射 ===")
mapped := strings.Map(func(r rune) rune {
if r == 'o' {
return '0'
}
return r
}, "Hello World")
fmt.Printf("Map: %v\n", mapped)
fmt.Println("\n=== 字符串生成器 ===")
var builder strings.Builder
builder.WriteString("Hello")
builder.WriteString(" ")
builder.WriteString("World")
fmt.Printf("Builder result: %v\n", builder.String())
fmt.Printf("Builder capacity: %v\n", builder.Cap())
fmt.Printf("Builder length: %v\n", builder.Len())
}
func customStringUtils() {
splitFunc := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
return
}
str := "custom,split;function"
scanner := bufio.NewScanner(strings.NewReader(str))
scanner.Split(splitFunc)
filter := func(r rune) bool {
return unicode.IsLetter(r) || unicode.IsNumber(r)
}
filtered := strings.Map(func(r rune) rune {
if filter(r) {
return r
}
return -1
}, "Hello123,World!")
transform := func(r rune) rune {
if unicode.IsLower(r) {
return unicode.ToUpper(r)
}
return unicode.ToLower(r)
}
transformed := strings.Map(transform, "Hello,World!")
}
1. 查找函数
strings.Contains(s, substr)
strings.ContainsAny(s, chars)
strings.ContainsRune(s, r)
strings.Index(s, substr)
strings.LastIndex(s, substr)
strings.IndexAny(s, chars)
strings.Count(s, substr)
2. 转换函数
strings.ToUpper(s)
strings.ToLower(s)
strings.Title(s)
strings.ToTitle(s)
strings.ToUpperSpecial(c, s)
3. 修剪函数
strings.TrimSpace(s)
strings.Trim(s, cutset)
strings.TrimLeft(s, cutset)
strings.TrimRight(s, cutset)
strings.TrimPrefix(s, prefix)
strings.TrimSuffix(s, suffix)
4. 分割和连接
strings.Split(s, sep)
strings.SplitN(s, sep, n)
strings.SplitAfter(s, sep)
strings.Fields(s)
strings.Join(elems, sep)
5. 替换函数
strings.Replace(s, old, new, n)
strings.ReplaceAll(s, old, new)
6. 比较函数
strings.Compare(a, b)
strings.EqualFold(s1, s2)
7. 前缀后缀检查
strings.HasPrefix(s, prefix)
strings.HasSuffix(s, suffix)
8. Builder(字符串构建器)
var builder strings.Builder
builder.WriteString("Hello")
builder.WriteString(" World")
result := builder.String()
9. 实用功能
strings.Repeat(s, count)
strings.Map(mapping func(rune) rune, s string)
strings.NewReader(s)
性能优化建议:
- 使用 Builder:
- 大量字符串拼接时使用 strings.Builder
- 避免使用 + 运算符连接字符串
- 高效查找:
- 使用 Contains 而不是 Index >= 0
- 使用 ContainsAny 替代多次 Contains
- 内存优化:
- Split 产生的切片会分配新内存
- 使用 SplitN 限制分割次数
- 合理使用 Fields 和 FieldsFunc
- 特殊场景:
- 处理大小写敏感问题用 EqualFold
- 处理 UTF-8 字符串使用 rune
- 需要重复拼接时使用 Repeat