Golang 分割字符串的实现示例

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

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

在 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

标签:Golang,分割字符串
0
投稿

猜你喜欢

  • js 删除数组的几种方法小结

    2024-04-22 22:42:37
  • Python接口测试文件上传实例解析

    2023-04-19 15:59:28
  • 前端面试运行npm run xxx发生过程原理解析

    2024-04-28 09:34:14
  • 解决Pytorch自定义层出现多Variable共享内存错误问题

    2023-12-14 14:43:46
  • oracle调试存储过程的过程详解

    2024-01-13 03:51:17
  • Python实现简单图像缩放与旋转

    2022-02-19 05:48:17
  • Django REST Framework 分页(Pagination)详解

    2022-07-25 04:21:06
  • Python urllib模块urlopen()与urlretrieve()详解

    2022-01-17 02:32:02
  • python 限制函数执行时间,自己实现timeout的实例

    2023-08-03 00:49:12
  • Python正则表达式学习小例子

    2023-01-30 21:57:42
  • 通过定位控制信息列表下往上的增加

    2008-06-30 14:27:00
  • python监控文件或目录变化

    2023-09-05 16:08:28
  • 使用matplotlib绘制并排柱状图的实战案例

    2022-04-06 01:22:25
  • ERROR 1222 (21000): The used SELECT statements have a different number of columns

    2024-01-15 02:31:28
  • MySQL自动为查询数据结果加序号

    2024-01-20 03:17:51
  • Go 实战单队列到优先级队列实现图文示例

    2024-05-22 10:19:03
  • Sql Server 2000内存调整

    2010-04-25 11:24:00
  • python实现Dijkstra静态寻路算法

    2021-06-10 07:41:09
  • python产生模拟数据faker库的使用详解

    2022-05-25 22:43:35
  • 一直闪烁变色的超级链接代码

    2008-02-27 13:08:00
  • asp之家 网络编程 m.aspxhome.com