funcmain() { b := 37 o := 41 x := 3931 d := 569 fmt.Printf("|%b|%9b|%-9b|%09b|\n", b, b, b, b) //|100101| 100101|100101 |000100101| fmt.Printf("|%o|%#o|%#8o|%#+8o|%+08o|\n", o, o, o, o, -o) //|51|051| 051| +051|-0000051| fmt.Printf("|%x|%X|%8x|%08x|%#04X|0x%04X|\n", x, x, x, x, x, x) //|f5b|F5B| f5b|00000f5b|0X0F5B|0x0F5B| fmt.Printf("|%d|%06d|%+06d|\n", d, d, d) //|569|000569|+00569| fmt.Printf("|%d|%#04x|%U|%c|\n", 0x3A6, 934, '\u03A6', '\U000003A6') //|934|0x03a6|U+03A6|Φ| }
strings
字符串分隔相关的函数
函数
描述
arr := strings.Split(string, “*”)
*为分隔符,返回一个字符串类型的切片
arr := strings.SplitN(string, “*”, n)
n为分隔后切片的长度,即分隔次数+1,从左到右
arr := strings.SplitAfter(string, “*”)
同Split,但返回的切片中字符串包含分隔符
arr := strings.SplitAfterN(string, “*”, n)
同SplitAfter,但只分隔n-1次,从左到右,n为分隔后切片的长度
arr := strings.Fields(string)
从字符串空白处切分字符串
arr := strings.FieldsFunc(string, f)
按照f返回为true的地方进行切分
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package main
import ( "fmt" "strings" )
funcmain() { str := "Honesty*and*diligence*should*be*your*eternal*mates." fmt.Println(strings.Split(str, "*")) //[Honesty and diligence should be your eternal mates.] fmt.Println(strings.SplitAfter(str, "*")) //[Honesty* and* diligence* should* be* your* eternal* mates.] fmt.Println(strings.SplitN(str, "*", 2)) //[Honesty and*diligence*should*be*your*eternal*mates.] fmt.Println(strings.SplitAfterN(str, "*", 2)) //[Honesty* and*diligence*should*be*your*eternal*mates.] }
字符串替换相关的函数
函数
描述
str := strings.Replace(str, old, new, n)
n为替换的次数,-1表示不限制
str := strings.Map(f, str)
按照func f(rune) rune{}规则替换str中的字符
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
package main
import ( "fmt" "strings" )
funcmain() { str := "Honesty*and*diligence*should*be*your*eternal*mates." fmt.Println(strings.Replace(str, "*", " ", -1)) //Honesty and diligence should be your eternal mates. fmt.Println(strings.Replace(str, "*", " ", 1)) //Honesty and*diligence*should*be*your*eternal*mates. fmt.Println(strings.Map(func(c rune)rune { if c == '*' { return' ' } return c }, str)) //Honesty and diligence should be your eternal mates. }
funcmain() { str := " Honesty and diligence should be your eternal mates. " str1 := "**Honesty and diligence should*be your eternal mates.**" fmt.Println(strings.TrimSpace(str)) //Honesty and diligence should be your eternal mates. fmt.Println(strings.TrimLeft(str1, "*")) //Honesty and diligence should*be your eternal mates.** fmt.Println(strings.TrimRight(str1, "*")) //**Honesty and diligence should*be your eternal mates. fmt.Println(strings.Trim(str1, "*")) //Honesty and diligence should*be your eternal mates. fmt.Println(strings.TrimFunc(str1, func(c rune)bool { switch c { case'*': returntrue } returnfalse })) //Honesty and diligence should*be your eternal mates. }
字符串判断相关的函数
函数
描述
b := strings.Contains(s, t)
如果s中包含t,则返回true
nr := strings.Count(s, t)
t在s中出现的次数
b := strings.EqualFold(s, t)
如果字符串s与t相等的话返回true,区分大小写
b := strings.HasPrefix(s, t)
如果s以t开头,则返回true
b := strings.HasSuffix(s, t)
如果s以t结尾,则返回true
idx := strings.Index(s, t)
t在s中第一次出现的索引位置
idx := strings.IndexAny(s, t)
t中的任意字符在s中第一次出现的索引位置
idx := strings.IndexFunc(s, f)
s中第一次令f返回true的索引位置
idx := strings.IndexRune(s, char)
字符char在s中第一次出现的索引位置
idx := strings.LastIndex(s, t)
t在s中最后一次出现的索引位置
idx := strings.LastIndexAny(s, t)
t中的任意字符在s中最后一次出现的索引位置
idx := strings.LastIndexFunc(s, f)
s中最后一次令f返回true的索引位置
idx := strings.LastIndexRune(s, char)
字符char在s中最后一次出现的索引位置
例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package main
import ( "fmt" "strings" )
funcmain() { s := "hello world" t := "world" fmt.Println(strings.Index(s, t)) //6 fmt.Println(strings.IndexAny(s, t)) //2 fmt.Println(strings.IndexRune(s, 'e')) //1 }
funcmain() { str := "Honesty and diligence should be your eternal mates." str1 := "wow" str2 := "Önnek İş" arr := []string{"Honesty", "and", "diligence", "should", "be", "your", "eternal", "mates."} fmt.Println(strings.Join(arr, " ")) //Honesty and diligence should be your eternal mates. fmt.Println(strings.Repeat(str1, 3)) //wowwowwow fmt.Println(strings.Title(str)) //Honesty And Diligence Should Be Your Eternal Mates. fmt.Println(strings.ToTitle(str)) //HONESTY AND DILIGENCE SHOULD BE YOUR ETERNAL MATES. fmt.Println(strings.ToLower(str)) //honesty and diligence should be your eternal mates. fmt.Println(strings.ToUpper(str)) //HONESTY AND DILIGENCE SHOULD BE YOUR ETERNAL MATES. fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, str2)) //önnek iş }
funcmain() { // constant with mixed type runes const mixed = "\b5Ὂg̀9! ℃ᾭG十" for _, c := range mixed { fmt.Printf("For %q:\n", c) if unicode.IsControl(c) { fmt.Println("\tis control rune") } if unicode.IsDigit(c) { fmt.Println("\tis digit rune") } if unicode.IsGraphic(c) { fmt.Println("\tis graphic rune") } if unicode.IsLetter(c) { fmt.Println("\tis letter rune") } if unicode.IsLower(c) { fmt.Println("\tis lower case rune") } if unicode.IsMark(c) { fmt.Println("\tis mark rune") } if unicode.IsNumber(c) { fmt.Println("\tis number rune") } if unicode.IsPrint(c) { fmt.Println("\tis printable rune") } if !unicode.IsPrint(c) { fmt.Println("\tis not printable rune") } if unicode.IsPunct(c) { fmt.Println("\tis punct rune") } if unicode.IsSpace(c) { fmt.Println("\tis space rune") } if unicode.IsSymbol(c) { fmt.Println("\tis symbol rune") } if unicode.IsTitle(c) { fmt.Println("\tis title case rune") } if unicode.IsUpper(c) { fmt.Println("\tis upper case rune") } } }
For '\b': is control rune is not printable rune For '5': is digit rune is graphic rune is number rune is printable rune For 'Ὂ': is graphic rune is letter rune is printable rune is upper case rune For 'g': is graphic rune is letter rune is lower case rune is printable rune For '̀': is graphic rune is mark rune is printable rune For '9': is digit rune is graphic rune is number rune is printable rune For '!': is graphic rune is printable rune is punct rune For ' ': is graphic rune is printable rune is space rune For '℃': is graphic rune is printable rune is symbol rune For 'ᾭ': is graphic rune is letter rune is printable rune is title case rune For 'G': is graphic rune is letter rune is printable rune is upper case rune For '十': is graphic rune is letter rune is printable rune