温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Golang怎么分割字符串

发布时间:2023-04-25 11:04:26 来源:亿速云 阅读:67 作者:iii 栏目:开发技术

本文小编为大家详细介绍“Golang怎么分割字符串”,内容详细,步骤清晰,细节处理妥当,希望这篇“Golang怎么分割字符串”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

1.按空格分割

ss := strings.Fields(s)

示例:

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("Fields are: %q", strings.Fields("  foo bar  baz   "))
}

输出:

Fields are: ["foo" "bar" "baz"]

2.按字符/字符串分割

ss := strings.Split(s, sep)

可以指定一个字符串作为分隔符,可以指定单个字符作为分隔符,因为单个字符也是一个字符串。

示例:

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("%q\n", strings.Split("a,b,c", ","))
    fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
    fmt.Printf("%q\n", strings.Split(" xyz ", ""))
    fmt.Printf("%q\n", strings.Split("", ","))
}

输出:

["a" "b" "c"]
["" "man " "plan " "canal panama"]
[" " "x" "y" "z" " "]
[""]

注意,如果待分割串为空串,strings.Split 将返回包含一个空串的切片。

如果希望返回 nil 切片,可以做一下封装。

func Split(s, sep string) []string {
    if s == "" {
        return nil
    }
    return strings.Split(s, sep)
}

3.按多个字符分割

标准库 strings 包中有一个函数 FieldsFunc 可以指定多个字符为分隔符。

ss := strings.FieldsFunc(s,f func(rune) bool)

示例:

package main

import (
    "fmt"
    "strings"
    "unicode"
)

func main() {
    f := func(c rune) bool {
        return !unicode.IsLetter(c) && !unicode.IsNumber(c)
    }
    fmt.Printf("Fields are: %q", strings.FieldsFunc("  foo1;bar2,baz3...", f))
}

输出:

Fields are: ["foo1" "bar2" "baz3"]

4.按多个字符串分割

截至 Go 1.20,标准库暂未供支持多个字符串作为分隔符的分割函数。但是,我们可以基于 strings.Split 编写一个函数来实现这个功能。

// SplitSeps splits string into substring slice by multiple string separators.
// If you want to specify multiple string separators by regexp,
// please refer to `func (*Regexp) Split` in standard library regexp package.
func SplitSeps(s string, seps ...string) []string {
    if len(seps) == 0 {
        return []string{s}
    }

    result := strings.Split(s, seps[0])
    for _, sep := range seps[1:] {
        var temp []string
        for _, r := range result {
            temp = append(temp, strings.Split(r, sep)...)
        }
        result = temp
    }
    return result
}

示例:

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Printf("%q\n", SplitSeps("foo,bar,baz", []string{","}...))
    fmt.Printf("%q\n", SplitSeps("foo,bar|baz", []string{",", "|"}...))
    fmt.Printf("%q\n", SplitSeps("foo,bar|baz qux", []string{",", "|", " "}...))
    fmt.Printf("%q\n", SplitSeps("foo,bar|bazSEPqux", []string{",", "|", "SEP"}...))
    fmt.Printf("%q\n", SplitSeps("foo,bar|baz", []string{}...))
    fmt.Printf("%q\n", SplitSeps(" xyz", []string{""}...))
}

输出:

["foo" "bar" "baz"]
["foo" "bar" "baz"]
["foo" "bar" "baz" "qux"]
["foo" "bar" "baz" "qux"]
["foo,bar|baz"]
[" " "x" "y" "z"]

5.其他分割函数

除了文中提及的标准库函数,你可能还会用到下面这几个函数来控制字符串的分割方式。

strings.SplitN

func strings.SplitN(s, sep string, n int) []string

该函数与 Split 函数类似,但是可以指定分割后的最大子字符串个数 n,如果 n 为正数,则最多分割成 n 个子字符串;如果 n 为负数,则不限制子字符串个数。例如:

str := "hello,world,how,are,you"
words := strings.SplitN(str, ",", 2)
fmt.Println(words) // ["hello", "world,how,are,you"]

strings.SplitAfter

func SplitAfter(s, sep string) []string

该函数将字符串 s 按照分割符 sep 分割成多个子字符串,并返回一个字符串切片。不同于 Split 函数的是,SplitAfter 函数会在分割符之后分割,例如:

str := "hello,world,how,are,you"
words := strings.SplitAfter(str, ",")
fmt.Println(words) // ["hello,", "world,", "how,", "are,", "you"]

strings.SplitAfterN

func SplitAfterN(s, sep string, n int) []string

该函数与 SplitAfter 函数类似,但是可以指定分割后的最大子字符串个数 n。如果 n 为负数,则不限制子字符串个数。例如:

str := "hello,world,how,are,you"
words := strings.SplitAfterN(str, ",", 2)
fmt.Println(words) // ["hello,", "world,how,are,you"]

6.go-huge-util

借助 Golang 标准库提供的相关函数,分割字符串还是比较方便的。

文中提及的两个函数,已放置 Github 开源工具库 go-huge-util,大家可使用 go mod 方式 import 然后使用。

import "github.com/dablelv/go-huge-util/str"

// Split 如果待分割串为空串,返回 nil 切片而非包含一个空串的切片。
str.Split(s, sep string) []string

// SplitSeps 通过多个字符串分隔符将字符串分割为字符串切片。
str.SplitSeps(s string, seps []string) []string

go-huge-util 除了类型转换,还有很多其他实用函数,如加解密、zip 解压缩等,欢迎大家使用、Star、Issue 和 Pull Request。

读到这里,这篇“Golang怎么分割字符串”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI