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.dablelv/cyan

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

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

import "github.com/dablelv/cyan/str"

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

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

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


参考文献

strings - Go Packages
8 ways to split a string in Go (Golang) - GOSAMPLES
github.com/dablelv/cyan
OpenAI ChatGPT

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值