string分割类函数
package main import ( "fmt" "strings" "unicode" ) /* 1、func Fields(s string) []string 将字符串s以空白字符分割,返回一个切片 2、func FieldsFunc(s string, f func(rune) bool) []string 将字符串s以满足f(r)==true的字符分割,返回一个切片 3、func Split(s, sep string) []string 将字符串s以sep作为分隔符进行分割,分割后字符最后去掉sep 4、func SplitAfter(s, sep string) []string 将字符串s以sep作为分隔符进行分割,分割后字符最后附上sep 5、func SplitAfterN(s, sep string, n int) []string 将字符串s以sep作为分隔符进行分割,分割后字符最后附上sep,n决定返回的切片数 6、func SplitN(s, sep string, n int) []string 将字符串s以sep作为分隔符进行分割,分割后字符最后去掉sep,n决定返回的切片数 */ func main() { TestSplitAfterN() } func TestFields() { fmt.Println(strings.Fields(" abc 123 ABC xyz XYZ")) //[abc 123 ABC xyz XYZ] } func TestFieldsFunc() { f := func(c rune) bool { //return c == '=' return !unicode.IsLetter(c) && !unicode.IsNumber(c) } fmt.Println(strings.FieldsFunc("abc@123*ABC&xyz%XYZ" , f)) //[abc 123 ABC xyz XYZ] } func TestSplit() { fmt.Printf("%q\n", strings.Split("a,b,c", ","))//[a b c] fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))//["" "man " "plan " "canal panama"] fmt.Printf("%q\n", strings.Split(" xyz ", ""))//[" " "x" "y" "z" " "] fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))//[""] } func TestSplitN() { fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2))//["a" "b,c"] fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 1))//["a,b,c"] } func TestSplitAfter() { fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ","))//["a," "b," "c"] } func TestSplitAfterN() { fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2))//["a," "b,c"] }
-
本文链接: https://dreamerjonson.com/2018/11/30/golang-23-string-package2/
-
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- PHP 字符串分割成数组函数 explode, str_split 内部实现
- 【图像分割】图像分割专栏栏主自述:分割,我们究竟在研究什么?
- 【图像分割模型】用BRNN做分割—ReSeg
- 语义分割:GSCNN 提高边缘和小目标的分割性能
- 语义分割领域开山之作:Google提出用神经网络搜索实现语义分割
- 【图像分割模型】BRNN下的RGB-D分割—LSTM-CF
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Head First HTML5 Programming
Eric Freeman、Elisabeth Robson / O'Reilly Media / 2011-10-18 / USD 49.99
What can HTML5 do for you? If you're a web developer looking to use this new version of HTML, you might be wondering how much has really changed. Head First HTML5 Programming introduces the key featur......一起来看看 《Head First HTML5 Programming》 这本书的介绍吧!