1. EqualFold
函數:func EqualFold(s, t string) bool
釋義: 判斷兩個utf-8編碼字元串(将unicode大寫、小寫、标題三種格式字元視為相同)是否相同。
例子:
s := "hello"
fmt.Println("string: ", s)
// 1、EqualFold 判斷兩個utf-8編碼字元串 是否相同。
f := strings.EqualFold(s, "Hello")
fmt.Println("EqualFold: ", f)
// EqualFold: true
2. HasPrefix
函數:func HasPrefix(s, prefix string) bool
釋義: 判斷s是否有字首字元串prefix。
例子:
// 2、HasPrefix 判斷s是否有字首字元串prefix。
f = strings.HasPrefix(s, "he")
fmt.Println("HasPrefix: ", f)
// HasPrefix: true
3. HasSuffix
函數:func HasSuffix(s, suffix string) bool
釋義: 判斷s是否有字尾字元串suffix。
例子:
// 3、HasSuffix 判斷s是否有字尾字元串suffix。
f = strings.HasSuffix(s, "llo")
fmt.Println("HasSuffix: ", f)
// HasSuffix: true
4. Contains
函數:func Contains(s, substr string) bool
釋義: 判斷字元串s是否包含子串substr。
例子:
// 4、Contains 判斷字元串s是否包含子串substr。
f = strings.Contains(s, "ll")
fmt.Println("Contains: ", s)
// Contains: hello
5. ContainsAny
函數:func ContainsAny(s, chars string) bool
釋義: 判斷字元串s是否包含字元串chars中的任一字元。
例子:
// 5、ContainsAny 判斷字元串s是否包含字元串chars中的任一字元。
f = strings.ContainsAny(s, "h")
f2 := strings.ContainsAny(s, "mp")
fmt.Println("ContainsAny: ", f, f2)
// ContainsAny: true false
6. Count
函數:func Count(s, sep string) int
釋義: 傳回字元串s中有幾個不重複的sep子串。
例子:
// 6、Count 傳回字元串s中有幾個不重複的sep子串。
c := strings.Count(s, "l")
fmt.Println("Count: ", c)
// Count: 2
7. Index
函數:func Index(s, sep string) int
釋義: 子串sep在字元串s中第一次出現的位置,不存在則傳回-1。
例子:
// 7、Index 子串sep在字元串s中第一次出現的位置,不存在則傳回-1。
i := strings.Index(s,"l")
fmt.Println("Index: ", i)
// Index: 2
8. IndexAny
函數:func IndexAny(s, chars string) int
釋義: 字元串chars中的任一utf-8碼值在s中第一次出現的位置,如果不存在或者chars為空字元串則傳回-1。
例子:
// 8、IndexAny 字元串chars中的任一utf-8碼值在s中第一次出現的位置,如果不存在或者chars為空字元串則傳回-1。
i = strings.IndexAny(s, "hmp")
fmt.Println("IndexAny: ", i)
// IndexAny: 0
9. LastIndex
函數:func LastIndex(s, sep string) int
釋義: 子串sep在字元串s中最後一次出現的位置,不存在則傳回-1。
例子:
// 9、LastIndex 子串sep在字元串s中最後一次出現的位置,不存在則傳回-1。
i = strings.LastIndex(s, "l")
fmt.Println("LastIndex: ", i)
// LastIndex: 3
10. Title
函數:func Title(s string) string
釋義: 傳回s中每個單詞的首字母都改為标題格式的字元串拷貝。
例子:
// 10、Title 傳回s中每個單詞的首字母都改為标題格式的字元串拷貝。
s = strings.Title(s)
fmt.Println("Title: ", s)
// Title: Hello
11. ToLower
函數:func ToLower(s string) string
釋義: 傳回将所有字母都轉為對應的小寫版本的拷貝。
例子:
// 11、ToLower 傳回将所有字母都轉為對應的小寫版本的拷貝。
s = "HELLO"
s = strings.ToLower(s)
fmt.Println("ToLower: ", s)
// ToLower: hello
12. ToUpper
函數:func ToUpper(s string) string
釋義: 傳回将所有字母都轉為對應的大寫版本的拷貝。
例子:
// 12、ToUpper 傳回将所有字母都轉為對應的大寫版本的拷貝。
s = "hello world"
s = strings.ToUpper(s)
fmt.Println("ToUpper: ", s)
// ToUpper: HELLO WORLD
13. Replace
函數:func Replace(s, old, new string, n int) string
釋義: 傳回将s中前n個不重疊old子串都替換為new的新字元串,如果n<0會替換所有old子串。
例子:
// 13、Replace 傳回路徑字元串中的卷名
s = "hello"
k := strings.Replace(s, "h", "k", -1)
fmt.Println("Replace: ", k)
// Replace: kello
14. Trim
函數:func Trim(s string, cutset string) string
釋義: 傳回将s前後端所有cutset包含的utf-8碼值都去掉的字元串。
例子:
// 14、Trim 根據pattern來判斷name是否比對,如果比對則傳回true
s = "!!! Achtung! Achtung! !!!"
sList := strings.Trim(s, "! ")
fmt.Println("Trim: ", sList)
// Trim: Achtung! Achtung
15. TrimSpace
函數:func TrimSpace(s string) string
釋義: 傳回将s前後端所有空白(unicode.IsSpace指定)都去掉的字元串。
例子:
// 15、TrimSpace 傳回将s前後端所有空白(unicode.IsSpace指定)都去掉的字元串。
s = "hello "
s = strings.TrimSpace(s)
fmt.Println("TrimSpace: ", s)
// TrimSpace: hello
16. TrimPrefix
函數:func TrimPrefix(s, prefix string) string
釋義: 傳回去除s可能的字首prefix的字元串。
例子:
// 16、TrimPrefix 傳回去除s可能的字首prefix的字元串。
s = "hello world"
s = strings.TrimPrefix(s, "he")
fmt.Println("TrimPrefix: ", s)
// TrimPrefix: llo world
17. TrimSuffix
函數:func TrimSuffix(s, suffix string) string
釋義: 傳回去除s可能的字尾suffix的字元串。
例子:
// 17、TrimSuffix 傳回去除s可能的字首prefix的字元串。
s = "hello world"
s = strings.TrimSuffix(s, "ld")
fmt.Println("TrimSuffix: ", s)
// TrimSuffix: hello wor
18. Fields
函數:func Fields(s string) []string
釋義: 傳回将字元串按照空白分割的多個字元串。如果字元串全部是空白或者是空字元串的話,會傳回空切片。
例子:
// 18、Fields 傳回将字元串按照空白分割的多個字元串。
s = "hello world"
sList2 := strings.Fields(s)
fmt.Println("Fields: ", sList2)
// Fields: [hello world]
19. FieldsFunc
函數:func FieldsFunc(s string, f func(rune) bool) []string
釋義: 類似Fields,但使用函數f來确定分割符。
例子:
// 19、FieldsFunc 類似Fields,但使用函數f來确定分割符。
s = "hello world"
fu := func(c rune) bool {
return !unicode.IsLetter(c) && !unicode.IsNumber(c)
}
sList2 = strings.FieldsFunc(s, fu)
fmt.Println("FieldsFunc: ", sList2)
// FieldsFunc: [hello world]
20. Split
函數:func Split(s, sep string) []string
釋義: 用去掉s中出現的sep的方式進行分割,會分割到結尾,并傳回生成的所有片段組成的切片
例子:
// 20、Split 用去掉s中出現的sep的方式進行分割,會分割到結尾,并傳回生成的所有片段組成的切片
s = "hello,world, you"
sList2 = strings.Split(s, ",")
fmt.Println("Split: ", sList2)
// Split: [hello world you]
21. SplitN
函數:func SplitN(s, sep string, n int) []string
釋義: 用去掉s中出現的sep的方式進行分割,會分割到結尾,并傳回生成的所有片段組成的切片
例子:
// 21、SplitN 用去掉s中出現的sep的方式進行分割,會分割到結尾,并傳回生成的所有片段組成的切片
s = "hello;world;you"
sList2 = strings.SplitN(s, ";", 2)
fmt.Println("SplitN: ", sList2)
// SplitN: [hello world;you]
22. SplitAfter
函數:func SplitAfter(s, sep string) []string
釋義: 用從s中出現的sep後面切斷的方式進行分割,會分割到結尾,并傳回生成的所有片段組成的切片
例子:
// 22、SplitAfter 用從s中出現的sep後面切斷的方式進行分割,會分割到結尾
// 并傳回生成的所有片段組成的切片
s = "hello;world;you"
sList2 = strings.SplitAfter(s, ";")
fmt.Println("SplitAfter: ", sList2)
// SplitAfter: [hello; world; you]
23. Join
函數:func Join(a []string, sep string) string
釋義: 将一系列字元串連接配接為一個字元串,之間用sep來分隔。
例子:
// 23、Join 将一系列字元串連接配接為一個字元串,之間用sep來分隔。
sL := []string{"I", "love", "you"}
s = strings.Join(sL, ".")
fmt.Println("Join: ", s)
// Join: I.love.you