Golang strings包常用字符串操作函數

在編寫代碼的時候最常用到的就是字符串瞭,Golang 中的字符串統一使用 UTF-8 (屬於Unicode編碼的一種實現方式)進行編碼,本篇文章將結合具體實例對常用的字符串操作函數進行介紹。

func Contains

func Contains(s, substr string) bool

作用:判斷 substr 是否是 s 的子串,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsContains("Linux", "in"))
  fmtPrintln(stringsContains("Linux", "Unix"))
  fmtPrintln(stringsContains("Linux", ""))
  fmtPrintln(stringsContains("", ""))
}

輸出:
true
false
true
true

func HasPrefix

func HasPrefix(s, prefix string) bool

作用:字符串 s 是否以 prefix 為開頭,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsHasPrefix("Linux", "Lin"))
  fmtPrintln(stringsHasPrefix("Linux", "in"))
  fmtPrintln(stringsHasPrefix("Linux", ""))
}

輸出:
true
false
true

func HasSuffix

func HasSuffix(s, suffix string) bool

作用:判斷字符串 s 是否以 suffix 結尾,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsHasSuffix("Linux", "nux"))
  fmtPrintln(stringsHasSuffix("Linux", "ix"))
  fmtPrintln(stringsHasSuffix("Linux", ""))
}

輸出:
true
false
true

func Replace

func Replace(s, old, new string, n int) string

作用:返回 s 中前 n 個不重復的 old 子串替換為 new 子串的新字符串,如果 n < 0 ,則替換所有 old 子串,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsReplace("Linux is very very very good!", "very", "much", 2))
  fmtPrintln(stringsReplace("Linux is very very very good!", "very", "much", -1))
}

輸出:
Linux is much much very good!
Linux is much much much good!

func Split

func Split(s, sep string) []string

作用:返回將字符串 s 按 sep子串分割的字符串切片,sep 為空字符串時,將 s 分割為每一個 unicode 碼值的字符串切片。例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsSplit("Linux, Unix, Windows, Android", ", "))
  fmtPrintln(stringsSplit(" Linux is very very very good! ", " "))
}

輸出:返回的是字符串數組。
[Linux Unix Windows Android]
[ Linux is very very very good! ]

func ToLower

func ToLower(s string) string

作用:返回字符串 s 中字母轉小寫的拷貝,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsToLower("Linux, Unix, Windows, Android"))
  fmtPrintln(stringsToLower(" Linux is very very very good! "))
}

輸出:
linux, unix, windows, android
 linux is very very very good!

func ToUpper

func ToUpper(s string) string

作用:返回字符串 s 中字母轉大寫的拷貝,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsToUpper("Linux, Unix, Windows, Android"))
  fmtPrintln(stringsToUpper(" Linux is very very very good! "))
}

輸出:
LINUX, UNIX, WINDOWS, ANDROID
LINUX IS VERY VERY VERY GOOD! 

func Repeat

func Repeat(s string, count int) string

作用:返回 count 個字符串 s 相連接的字符串,如果 count 為負數 或(len * s * count)的結果溢出,則報 panic 異常。例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsRepeat("Linux", 6))
  fmtPrintln(stringsRepeat("Linux", 0))
  fmtPrintln(stringsRepeat("Linux", -1))
}

輸出:
LinuxLinuxLinuxLinuxLinuxLinux
 
panic: strings: negative Repeat count
 
goroutine 1 [running]:
stringsRepeat(0x4bcf3d, 0x5, 0xffffffffffffffff, 0x1, 0x1)
        /usr/local/go/src/strings/stringsgo:529 +0x5e5
mainmain()
        /root/goProject/src/test/maingo:11 +0x167

func Count

func Count(s, substr string) int

作用:返回字符串 s 中包含的不重疊的子串 substr 個數,如果 substr 是一個空字符串,則返回1 + s中的Unicode代碼點數(Unicode 代碼點:可以簡單理解為一個符號便是一個代碼點),例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsCount("Golang Golang Golang", "Go"))
  fmtPrintln(stringsCount("Golang", ""))
  fmtPrintln(stringsCount("Golang語言", ""))
}

輸出:
3
7
9

func Index

func Index(s, substr string) int

作用:返回字符串 s 中包含的第一個子串 substr 的索引,如果不存在返回 -1,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsIndex("Golang", "lang"))
  fmtPrintln(stringsIndex("Golang", "Linux"))
}

輸出:
2
-1

func Join

func Join(elems []string, sep string) string

作用:使用 sep 作為分隔符,將elems 中的所有字符連接起來,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  elems := []string{"I", "like", "golang", "!"}
  fmtPrintln(stringsJoin(elems, " "))
  elems = []string{"123", "456", "789"}
  fmtPrintln(stringsJoin(elems, "-"))
}

 輸出:
[root@localhost gotest]# go run maingo
I like golang !
123-456-789
[root@localhost gotest]#

到此這篇關於Golang strings包常用字符串操作函數的文章就介紹到這瞭,更多相關Golang strings包字符串操作內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: