Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize string cutting in Go language

Shulou Source: shulou.com Published: 2022-05-31 22:23:31 09月25日 Update

Today Xiaobian to share with you how to achieve string cutting in Go language related knowledge points, detailed content, clear logic, I believe most people still know too much about this knowledge, so share this article for everyone to refer to, I hope you read this article after harvest, let's learn about it together.

1. func Fields(s string) []string, This function divides the string according to 1: n spaces and finally returns

[] String of slices

import (

"fmt"

"strings"

)

func main() {

fmt.Println(strings.Fields("hello widuu golang")) //out [hello widuu golang]

}

2. func FieldsFunc(s string, f func(rune) bool) []string See at a glance, this is divided according to custom functions

The copy code is as follows:

import (

"fmt"

"strings"

)

func main() {

fmt.Println(strings.FieldsFunc("widuunhellonword", split)) // [widuu hello word] split by n characters

}

func split(s rune) bool {

if s == 'n' {

return true

}

return false

}

3. func Join(a []string, sep string) string, this is similar to implode in php, this function divides a slice of []string into a string by a delimiter

The copy code is as follows:

import (

"fmt"

"strings"

)

func main() {

s := []string{"hello", "word", "xiaowei"}

fmt.Println(strings.Join(s, "-")) // hello-word-xiaowei

}

4. func Split(s, sep string) []string, there is join there is Split This is to cut the string into slices according to the specified delimiter

The copy code is as follows:

import (

"fmt"

"strings"

)

func main() {

fmt.Println(strings.Split("a,b,c,d,e", ",")) //[a b c d e]

}

5. func SplitAfter(s, sep string) []string, this function is after the previous cut is completed and then add the sep separator

The copy code is as follows:

import (

"fmt"

"strings"

)

func main() {

fmt.Println(strings.SplitAfter("a,b,c,d", ",")) //[a, b, c, d]

}

6. func SplitAfterN(s, sep string, n int) []string This function s splits according to sep and returns the slice of the substring after splitting, just like split, except that the returned substring retains sep. If sep is empty, then each character is split.

The copy code is as follows:

import (

"fmt"

"strings"

)

func main() {

fmt.Println(strings.SplitAfterN("a,b,c,d,r", ",", 4)) //["a," "b," "c," "d,r"]

fmt.Println(strings.SplitAfterN("a,b,c,d,r", ",", 5)) //["a," "b," "c," "d," "r"]

}

7. func SplitN(s, sep string, n int) []string, This is the length defined when cutting the string. If sep is empty, then each character is split.

The copy code is as follows:

import (

"fmt"

"strings"

)

func main() {

fmt.Println(strings.SplitN("a,b,c", ",", 2)) //[a b,c]

}

That's all for "How to cut strings in Go". Thank you for reading! I believe everyone has a great harvest after reading this article. Xiaobian will update different knowledge for everyone every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Tags: Code characters strings functions knowledge articles that is languages content delimiters different big almost useful but most of the time in more knowledge points. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno vpn Xiaomi Apple Shulou Tech Info