网络编程
位置:首页>> 网络编程>> Go语言>> Golang 分割字符串的实现示例

Golang 分割字符串的实现示例

作者:恋喵大鲤鱼  发布时间:2024-02-23 03:26:10 

标签:Golang,分割字符串

在开发过程中,很多时候我们有分割字符串的需求,即把一个字符串按照某种分割符进行切割。

在 Go 语言中,分割字符串我们可以分为几种情况,分别为:

  • 按空格分割

  • 按字符分割

  • 按字符串分割

下面分别讲解使用 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。

参考文献

strings - Go Packages
8 ways to split a string in Go (Golang) - GOSAMPLES
github.com/dablelv/go-huge-util

来源:https://dablelv.blog.csdn.net/article/details/129302023

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com