网站首页 文章专栏 Go 语言 string 模块
Go 语言 string 模块
创建于:2021-07-04 08:45:48 更新于:2024-04-19 20:40:09 羽瀚尘 505
Go Go


[toc]

切割

go strings.Split(s, "_") strings.SplitN() // 指定切割次数 strings.SplitAfter() // 保留分隔符 strings.SplitAfterN() // 指定切割次数

连接

go strings.Join(strings.Split(s, "_"), "+")

替换

go strings.Replace(s, "_", ".", 1)

查找

go strings.Contains (s, t) //s 中是否包含 t strings.Index (s, t) //t 在 s 中出现的第一次索引的位置 strings.Count (s, t) //t 在 s 中出现的次数 // 检测字符串是否包含某个子串 func Contains(s, substr string) bool // 检测字符串是否包含某个字符 func ContainsRune(s string, r rune) bool // 检测字符串是否包含字符集中的某个字符 func ContainsAny(s, chars string) bool

去白

go strings.TrimSpace (s) // 返回去除字符串首尾空白的字符串 strings.Trim (s, t) // 去除 s 中 t 字符 strings.TrimLeft(s, t) strings.TrimRight(s, t)

大小写

go strings.ToLower (s) // 将字符串变为小写 strings.ToUpper (s) // 将字符串变为大写

比较

go // 通过字典序比较两个字符串的大小等价于>,<,==运算 func Compare(a, b string) int strings.repeat (s, i) // 重复 i 次 s 字符

与 int 转换

go // string 转 int int, err := strconv.Atoi(string) // string 转 int64 int64, err := strconv.ParseInt(string, 10, 64) // int 转 string string := strconv.Itoa(int) // int64 转 string string := strconv.FormatInt(int64,10)