Go中strings包的基本使用示例代碼
本篇主要總結(jié)的是go中的string包的一些函數(shù)的操作講解
string
在各個(gè)語言中,都有對應(yīng)的處理字符串的包,在go中是使用strings來處理的
前綴和后綴
HasPrefix() 判斷字符串 s 是否以 prefix 開頭:
strings.HasPrefix(s, prefix string) bool
HasSuffix() 判斷字符串 s 是否以 suffix 結(jié)尾:
strings.HasSuffix(s, suffix string) bool
示例代碼
func test1() {
fmt.Println(strings.HasPrefix("this is string", "this"))
fmt.Println(strings.HasPrefix("this is string", "1this"))
fmt.Println(strings.HasSuffix("this is string", "ing"))
fmt.Println(strings.HasSuffix("this is string", "iing"))
}字符串包含
Contains() 判斷字符串 s 是否包含 substr:
strings.Contains(s, substr string) bool
示例代碼
func test2() {
totalString := "hello go, i love cpp"
containString1 := "go"
containString2 := "cpp"
containString3 := "java"
fmt.Printf("\"%s\" contain in \"%s\"? the ans is %t\n", containString1, totalString, strings.Contains(totalString, containString1))
fmt.Printf("\"%s\" contain in \"%s\"? the ans is %t\n", containString2, totalString, strings.Contains(totalString, containString2))
fmt.Printf("\"%s\" contain in \"%s\"? the ans is %t\n", containString3, totalString, strings.Contains(totalString, containString3))
}這里順便溫習(xí)一下對于Go中轉(zhuǎn)義字符的使用
判斷子字符串或字符在父字符串中出現(xiàn)的位置
Index() 返回字符串 str 在字符串 s 中的索引(str 的第一個(gè)字符的索引),-1 表示字符串 s 不包含字符串 str:
strings.Index(s, str string) int
LastIndex() 返回字符串 str 在字符串 s 中最后出現(xiàn)位置的索引(str 的第一個(gè)字符的索引),-1 表示字符串 s 不包含字符串 str:
strings.LastIndex(s, str string) int
示例代碼:
func test3() {
totalString := "hello go, i love cpp, i love cpp and go"
containString1 := "go"
containString2 := "cpp"
containString3 := "java"
fmt.Println("first index demo is:")
fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString1, totalString, strings.Index(totalString, containString1))
fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString2, totalString, strings.Index(totalString, containString2))
fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString3, totalString, strings.Index(totalString, containString3))
fmt.Println("last index demo is:")
fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString1, totalString, strings.LastIndex(totalString, containString1))
fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString2, totalString, strings.LastIndex(totalString, containString2))
fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString3, totalString, strings.LastIndex(totalString, containString3))
}運(yùn)行結(jié)果為:
first index demo is:
"go" first index in "hello go, i love cpp, i love cpp and go" is 6
"cpp" first index in "hello go, i love cpp, i love cpp and go" is 17
"java" first index in "hello go, i love cpp, i love cpp and go" is -1
last index demo is:
"go" first index in "hello go, i love cpp, i love cpp and go" is 37
"cpp" first index in "hello go, i love cpp, i love cpp and go" is 29
"java" first index in "hello go, i love cpp, i love cpp and go" is -1
如果需要查詢非 ASCII 編碼的字符在父字符串中的位置,建議使用以下函數(shù)來對字符進(jìn)行定位:
strings.IndexRune(s string, r rune) int
字符串替換
Replace() 用于將字符串 str 中的前 n 個(gè)字符串 old 替換為字符串 new,并返回一個(gè)新的字符串,如果 n = -1 則替換所有字符串 old 為字符串 new:
strings.Replace(str, old, new string, n int) string
示例代碼
func test4() {
str1 := "hello hello hello hello hello hello"
str2 := strings.Replace(str1, "hello", "no", -1)
fmt.Println(str2)
}這個(gè)函數(shù)的意思就是只要識別到有可以替換的字符串,并且對于最后一個(gè)數(shù)字嗯,并沒有超過所限制的數(shù)量,那么就會將這個(gè)識別道德字符串替換為想要替換成的字符串,比如在這個(gè)例子當(dāng)中當(dāng)識別到字符串中含有哈嘍,這個(gè)單詞是就會將hello替換成no,前提是沒有超過-1的限制,而因?yàn)?1的意思是,只要有字符串就進(jìn)行替換,那么就會整個(gè)將這個(gè)字符串當(dāng)中所有含有hello的字符串都替換為no
統(tǒng)計(jì)字符串出現(xiàn)次數(shù)
Count() 用于計(jì)算字符串 str 在字符串 s 中出現(xiàn)的非重疊次數(shù):
strings.Count(s, str string) int
示例代碼
func test5() {
str1 := "hello world hello world hellhello world"
fmt.Println(strings.Count(str1, "hello"))
fmt.Println(strings.Count(str1, "world"))
}重復(fù)字符串
Repeat() 用于重復(fù) count 次字符串 s 并返回一個(gè)新的字符串:
strings.Repeat(s, count int) string
示例代碼
func test6() {
str := "hello go"
fmt.Println(strings.Repeat(str, 10))
fmt.Println(strings.Repeat(str, 2))
}修改字符串大小寫
ToLower() 將字符串中的 Unicode 字符全部轉(zhuǎn)換為相應(yīng)的小寫字符:
strings.ToLower(s) string
ToUpper() 將字符串中的 Unicode 字符全部轉(zhuǎn)換為相應(yīng)的大寫字符:
strings.ToUpper(s) string
示例代碼
func test7() {
str := "hello World This is TEST"
fmt.Println(strings.ToLower(str))
fmt.Println(strings.ToUpper(str))
}修剪字符串
你可以使用 strings.TrimSpace(s) 來剔除字符串開頭和結(jié)尾的空白符號;如果你想要剔除指定字符,則可以使用 strings.Trim(s, “cut”) 來將開頭和結(jié)尾的 cut 去除掉。該函數(shù)的第二個(gè)參數(shù)可以包含任何字符,如果你只想剔除開頭或者結(jié)尾的字符串,則可以使用 TrimLeft() 或者 TrimRight() 來實(shí)現(xiàn)。
示例代碼
func test8() {
str1 := "11hello world111"
str2 := " hello go "
fmt.Println("去除空白")
fmt.Println(strings.TrimSpace(str1))
fmt.Println(strings.TrimSpace(str2))
fmt.Println("去除左側(cè)空白")
fmt.Println(strings.TrimLeft(str2, " "))
fmt.Println("去除左側(cè)字符1")
fmt.Println(strings.TrimLeft(str1, "1"))
fmt.Println("去除右側(cè)字符1")
fmt.Println(strings.TrimRight(str1, "1"))
fmt.Println("去除左右兩側(cè)1")
fmt.Println(strings.Trim(str1, "1"))
}分割字符串
strings.Fields(s) 將會利用 1 個(gè)或多個(gè)空白符號來作為動態(tài)長度的分隔符將字符串分割成若干小塊,并返回一個(gè) slice,如果字符串只包含空白符號,則返回一個(gè)長度為 0 的 slice。
strings.Split(s, sep) 用于自定義分割符號來對指定字符串進(jìn)行分割,同樣返回 slice。
因?yàn)檫@ 2 個(gè)函數(shù)都會返回 slice,所以習(xí)慣使用 for-range 循環(huán)來對其進(jìn)行處理
示例代碼
func test9() {
str1 := "hello1 hello2 hello3 hello4"
fmt.Println("以一個(gè)空格為分隔符")
s1 := strings.Split(str1, " ")
for _, t := range s1 {
fmt.Println(t)
}
fmt.Println("以一個(gè)或多個(gè)空格為分隔符")
s2 := strings.Fields(str1)
for _, t := range s2 {
fmt.Println(t)
}
str3 := "hello11hello22hello321321312hello4"
fmt.Println("以hello為分隔符")
s3 := strings.Split(str3, "hello")
for _, t := range s3 {
fmt.Println(t)
}
}拼接 slice 到字符串
Join() 用于將元素類型為 string 的 slice 使用分割符號來拼接組成一個(gè)字符串:
strings.Join(sl []string, sep string) string
示例代碼
func test10() {
// 定義一個(gè)字符串切片
sl := []string{"apple", "banana", "cherry"}
// 使用空格作為分隔符拼接切片中的字符串
result := strings.Join(sl, " ")
fmt.Println(result) // 輸出 "apple banana cherry"
// 使用逗號和空格作為分隔符拼接切片中的字符串
result2 := strings.Join(sl, ", ")
fmt.Println(result2) // 輸出 "apple, banana, cherry"
}strconv
string類型的數(shù)據(jù)和其他類型的數(shù)據(jù)進(jìn)行轉(zhuǎn)換,實(shí)際上是借助這個(gè)包來完成的
這里只講述最基本的內(nèi)容,其他的內(nèi)容之后再進(jìn)行講解
示例代碼:
func test11() {
str1 := "666"
number, _ := strconv.Atoi(str1)
fmt.Println(number)
fmt.Println(strconv.Itoa(number + 5))
}到此這篇關(guān)于Go:strings包的基本使用的文章就介紹到這了,更多相關(guān)Go strings包使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
golang?recover函數(shù)使用中的一些坑解析
這篇文章主要為大家介紹了golang?recover函數(shù)使用中的一些坑解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
golang包循環(huán)引用的幾種解決方案總結(jié)
golang有包循環(huán)引用問題,用過的應(yīng)該都知道,下面這篇文章主要給大家介紹了關(guān)于golang包循環(huán)引用的幾種解決方案,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
詳解golang 定時(shí)任務(wù)time.Sleep和time.Tick實(shí)現(xiàn)結(jié)果比較
本文主要介紹了golang 定時(shí)任務(wù)time.Sleep和time.Tick實(shí)現(xiàn)結(jié)果比較,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02

