Go 语言的 strings 包提供了丰富的字符串处理函数,以下是一些常用函数的分类介绍及示例:
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
var sb strings.Builder // 创建一个strings.Builder实例
sb.WriteString("Hello, ") // 向Builder中写入字符串
sb.WriteString("World!") // 再次写入字符串
fmt.Println(sb.String()) // 从Builder中获取最终的字符串
sb2 := strings.Clone(sb.String()) // 克隆Builder中的字符串
fmt.Println(sb2) // 输出克隆的字符串
fmt.Println(strings.Compare("abc", "abc")) // 比较两个字符串,返回0表示相等 0
fmt.Println(strings.Compare("abc", "abd")) // 比较两个字符串,返回负数表示前者小于后者 -1
fmt.Println(strings.Compare("abd", "abc")) // 比较两个字符串,返回正数表示前者大于后者 1
fmt.Println(strings.Contains("Hello, World!", "World")) // 检查字符串是否包含子串,返回true
fmt.Println(strings.ContainsAny("Hello, World!", "llo")) // 检查字符串是否包含任何指定字符,返回true
fmt.Println(strings.ContainsRune("Hello, World!", 'W')) // 检查字符串是否包含指定的Unicode字符,返回true
fmt.Println(strings.Count("Hello, World!", "o")) // 计算子串在字符串中出现的次数,返回2
fmt.Println(strings.Cut("Hello, World!", "o")) // 从字符串中移除指定的子串,返回移除子串后的字符串和是否成功移除的布尔值 ("Hell, World!" true)
fmt.Println(strings.EqualFold("hello", "HELLO")) // 忽略大小写比较两个字符串,返回true
fmt.Println(strings.Fields("Hello, World! This is Go.")) // 将字符串按空白字符分割成子串切片,返回切片
fmt.Println(strings.FieldsFunc("Hello, World! This is Go.", func(r rune) bool { // 将字符串按指定的函数分割成子串切片,返回切片
return r == ' ' || r == '!' || r == '.'
}))
fmt.Println(strings.HasPrefix("Hello, World!", "Hello")) // 检查字符串是否以指定的前缀开头,返回true
fmt.Println(strings.HasSuffix("Hello, World!", "World!")) // 检查字符串是否以指定的后缀结尾,返回true
fmt.Println(strings.Index("Hello, World!", "o")) // 查找子串在字符串中第一次出现的位置,返回4
fmt.Println(strings.IndexAny("Hello, World!", "oW")) // 查找字符串中首次出现指定字符的位置,返回4
fmt.Println(strings.IndexByte("Hello, World!", 'o')) // 查找字节在字符串中第一次出现的位置,返回4
fmt.Println(strings.IndexFunc("Hello, World!", func(r rune) bool { // 查找字符串中首次满足指定函数条件的字符位置,返回1
return r >= 'A' && r <= 'Z'
}))
fmt.Println(strings.IndexRune("Hello, World!", 'o')) // 查找Unicode字符在字符串中第一次出现的位置,返回4
fmt.Println(strings.Join([]string{"Hello", "World", "Go"}, ", ")) // 将字符串切片连接成一个字符串,使用指定的分隔符,返回"Hello, World, Go"
fmt.Println(strings.LastIndex("Hello, World!", "o")) // 查找子串在字符串中最后一次出现的位置,返回8
fmt.Println(strings.LastIndexAny("Hello, World!", "oW")) // 查找字符串中最后一次出现指定字符的位置,返回8
fmt.Println(strings.LastIndexByte("Hello, World!", 'o')) // 查找字节在字符串中最后一次出现的位置,返回8
fmt.Println(strings.LastIndexFunc("Hello, World!", func(r rune) bool { // 查找字符串中最后一次满足指定函数条件的字符位置,返回7
return r >= 'A' && r <= 'Z'
}))
fmt.Println(strings.Map(func(r rune) rune { // 使用指定的函数映射字符串中的每个字符,返回映射后的字符串 "hELLO, wORLD!"
if r >= 'a' && r <= 'z' {
return r - 32 // 转换为大写
} else if r >= 'A' && r <= 'Z' {
return r + 32 // 转换为小写
}
return r // 其他字符不变
}, "Hello, World!"))
fmt.Println(strings.NewReader("Hello, World!").Len()) // 创建一个新的字符串读取器,并获取其长度,返回13
fmt.Println(strings.NewReplacer("a", "A", "b", "B")) // 创建一个新的字符串替换器,并返回一个指针)
fmt.Println(strings.NewReplacer("a", "A", "b", "B").Replace("abcabc")) // 使用替换器替换字符串中的指定子串,返回"ABcABc"
fmt.Println(strings.Reader{}) // 创建一个空的字符串读取器
fmt.Println(strings.Repeat("Go!", 3)) // 重复字符串指定次数,返回"Go!Go!Go!"
fmt.Println(strings.Replace("Hello, World! World!", "World", "Go", 1)) // 替换字符串中指定子串的前N次出现,返回"Hello, Go! World!"
fmt.Println(strings.ReplaceAll("Hello, World! World!", "World", "Go")) // 替换字符串中所有指定子串,返回"Hello, Go! Go!"
// 正确用法:创建一个替换器实例再调用Replace方法
replacer := strings.NewReplacer("World", "Go")
fmt.Println(replacer.Replace("Hello, World! World!")) // 返回"Hello, Go! Go!"
fmt.Println(strings.Split("a,b,c,d,e", ",")) // 将字符串按指定分隔符分割成子串切片,返回切片 ["a" "b" "c" "d" "e"]
fmt.Println(strings.SplitAfter("a,b,c,d,e", ",")) // 将字符串按指定分隔符分割成子串切片,包含分隔符,返回切片 ["a," "b," "c," "d," "e,"]
fmt.Println(strings.SplitAfterN("a,b,c,d,e", ",", 3)) // 将字符串按指定分隔符分割成子串切片,包含分隔符,最多分割N次,返回切片 ["a," "b," "c,d,e"]
fmt.Println(strings.SplitN("a,b,c,d,e", ",", 3)) // 将字符串按指定分隔符分割成子串切片,最多分割N次,返回切片 ["a" "b" "c,d,e"]
fmt.Println(strings.ToLower("ABC")) // 将字符串转换为小写,返回"abc"
fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "I")) // 使用指定的语言规则将字符串转换为小写,返回"ı"
fmt.Println(strings.ToTitle("hello")) // 将字符串转换为标题格式,返回"HELLO"
fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "i")) // 使用指定的语言规则将字符串转换为标题格式,返回"İ"
fmt.Println(strings.ToUpper("abc")) // 将字符串转换为大写,返回"ABC"
fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "ı")) // 使用指定的语言规则将字符串转换为大写,返回"İ"
fmt.Println(strings.ToValidUTF8("a\x00b", "?")) // 将字符串中的无效UTF-8序列替换为指定的替换字符串,返回"a?b"
fmt.Println(strings.Trim(" Hello, World! ", " ")) // 去除字符串两端的指定字符,返回"Hello, World!"
fmt.Println(strings.TrimFunc(" Hello, World! ", func(r rune) bool { // 去除字符串两端满足指定函数条件的字符,返回"Hello, World!"
return r == ' ' || r == '!'
}))
fmt.Println(strings.TrimLeft(" Hello, World! ", " ")) // 去除字符串左端的指定字符,返回"Hello, World! "
fmt.Println(strings.TrimLeftFunc(" Hello, World! ", func(r rune) bool { // 去除字符串左端满足指定函数条件的字符,返回"Hello, World! "
return r == ' ' || r == '!'
}))
fmt.Println(strings.TrimPrefix("Hello, World!", "Hello, ")) // 去除字符串的指定前缀,返回"World!"
fmt.Println(strings.TrimRight(" Hello, World! ", " ")) // 去除字符串右端的指定字符,返回" Hello, World
fmt.Println(strings.TrimRightFunc(" Hello, World! ", func(r rune) bool { // 去除字符串右端满足指定函数条件的字符,返回" Hello, World!"
return r == ' ' || r == '!'
}))
fmt.Println(strings.TrimSpace(" Hello, World! ")) // 去除字符串两端的空白字符,返回"Hello, World!"
fmt.Println(strings.TrimSuffix("Hello, World!", " World!")) // 去除字符串的指定后缀,返回"Hello"
}

355

被折叠的 条评论
为什么被折叠?



