首頁  >  文章  >  後端開發  >  總結分享Go中常用的strings函數

總結分享Go中常用的strings函數

藏色散人
藏色散人轉載
2021-10-20 15:55:482014瀏覽

本文由go語言教學專欄為大家總結分享Golang 常用的 strings 函數,希望對需要的朋友有幫助!

總結分享Go中常用的strings函數

Golang 常用的strings 函數

函數 簡介
len(str) 1.  統計字串長度,按位元組len(str)

2.  字串遍歷,處理中文r:=[]rune(str)

3.  字串轉整數n, err := strconv.Atoi("12")

4.  整數轉字串str = strconv.Itoa(12345)

5.  字串 轉 []byte  var bytes =  []byte("hello go")

6 .  []byte  轉 字串str =  string([]byte{97,  98,  99})

7 .  10  進制轉 2,  8,  16  進制: str = strconv.FormatInt(123,  2)  // 2-> 8 , 16

8.  找出子字串是否在指定的字串中strings.Contains("seafood",  "foo")  //true

#9.  統計字串有幾個指定的子字串strings.Count("ceheese",  "e")  //4

#10. 不區分大小寫的字串比較(==是區分字母大小寫的) fmt.Println(strings.EqualFold(" abc",  "Abc"))  // true

#11. 返回子字串在字串第一次出現的index 值,如果沒有回傳-1 strings.Index("NLT_abc",  "abc")  // 4

##12. 回傳子字串最後一次出現的index,如沒有回傳-1 strings.LastIndex("go golang",  "go")

13. 將指定的子字串替換成 另一個子字串strings.Replace("go go hello",  "go",  "go 語言", n)  ,n 可指 定你希望替換幾個,如果n=-1  表示全部替換

#14. 依照指定的某個字符,為分割標識,將一個字串拆分成字串數組strings.Split("hello,wrold,ok",  ",")

15. 將字串的字母進行大小寫的轉換: strings.ToLower("Go")  // go strings.ToUpper("Go") // GO

#16. 將字串左右兩邊的空格去掉: strings.TrimSpace(" tn a lone gopher ntrn ")

#17. 將字串左右兩邊指定的字元去掉 : strings.Trim("! hello! ",  " !")

18. 將字串左邊指定的字元去掉 : strings.TrimLeft("! hello! ",  " !")

19. 將字串右邊指定的字元去掉 :strings.TrimRight("! hello! ",  " !")

20. 判斷字串是否以指定的字串開頭: strings.HasPrefix("ftp://192.168.10.1 ",  "ftp")

#21. 判斷字串是否以指定的字串結束: strings.HasSuffix( "NLT_abc.jpg",  "abc")  //false
#

以上是總結分享Go中常用的strings函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:learnku.com。如有侵權,請聯絡admin@php.cn刪除