In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.