[TOC]
## strings
### 字符串的比較
> func Compare(a, b string) int
Compare 函數,用于比較兩個字符串的大小,如果兩個字符串相等,返回為 0。如果 a 小于 b ,返回 -1 ,反之返回 1 。不推薦使用這個函數,直接使用 == != > < >= <= 等一系列運算符更加直觀。
```
s1 := "phper"
s2 := "goper"
b1 := strings.Compare(s1, s2)
fmt.Println(b1)//1
b2 := strings.Compare(s2, s1)
fmt.Println(b2)//-1
b3 := strings.Compare(s1, s1)
fmt.Println(b3)//0 只有為0時是相等的
```
>func EqualFold(s, t string) bool
EqualFold 函數,計算 s 與 t 忽略字母大小寫后是否相等。
```
s3 := "go"
s4 := "Go"
b4 := strings.EqualFold(s3,s4)
fmt.Println(b4)//true
s5 := "go"
s6 := "Go2"
b5 := strings.EqualFold(s5,s6)
fmt.Println(b5)//false
```
### 是否存在某個字符或子串
>func Contains(s, substr string) bool
子串 substr 在 s 中,返回 true
```
s := "go,haha"
fmt.Println(strings.Contains(s , "go")) //true
```
>func ContainsAny(s, chars string) bool
chars 中任何一個 Unicode 代碼點在 s 中,返回 true
```
s := "helloworld"
fmt.Println(strings.ContainsAny(s, "h"))//true
fmt.Println(strings.ContainsAny(s, "h & i"))//true
fmt.Println(strings.ContainsAny(s, "e g"))//true
fmt.Println(strings.ContainsAny(s, ""))//false
fmt.Println(strings.ContainsAny("", ""))//false
即任意一個字符在字符串中存在就返回true
```
>func ContainsRune(s string, r rune) bool
Unicode 代碼點 r 在 s 中,返回 true
```
```
### 子串出現次數
> func Count(s, sep string) int
```
s := "helloworld"
fmt.Println(strings.Count(s,"o")) //2
```
### 分割字符串
>func Split(s, sep string) []string
func SplitN(s, sep string, n int) []string
**Split(s, sep) 和 SplitN(s, sep, -1) 等價**
```
s := "愛,我,中,華"
fmt.Println(strings.Split(s,","))//[愛 我 中 華]
fmt.Println(strings.SplitN(s,",",1))//[愛,我,中,華]
fmt.Println(strings.SplitN(s,",",2))//[愛 我,中,華]
fmt.Println(strings.SplitN(s,",",3))//[愛 我 中,華]
fmt.Println(strings.SplitN(s,",",-1))//[愛 我 中 華]
```
可以看出參數中的 n 表示返回的切片中的元素
>func SplitAfter(s, sep string) []string
func SplitAfterN(s, sep string, n int) []string
**SplitAfter(s, sep) 和 SplitAfterN(s, sep, -1) 等價**
```
fmt.Println(strings.SplitAfter(s,","))//[愛, 我, 中, 華]
fmt.Println(strings.SplitAfterN(s,",",2))//[愛, 我,中,華]
```
可以看出,帶after的函數,在生成切片后,仍然保留了step,
### 切片->字符串
```
ss := []string{
"愛",
"我",
"中",
"華",
}
fmt.Println(strings.Join(ss,"&"))//愛&我&中&華
```
### 前綴
```
s := "helloworld"
fmt.Println(strings.HasPrefix(s, "go"))//false
fmt.Println(strings.HasPrefix(s, "he"))//true
```
### 后綴
```
s := "helloworld"
fmt.Println(strings.HasSuffix(s, "ha")) //false
fmt.Println(strings.HasSuffix(s, "ld")) //true
```
### 字符或子字符串在字符串中出現的位置
```
s := "helloworld"
fmt.Println(strings.Index(s, "l"))//2
fmt.Println(strings.LastIndex(s, "l"))//8
```
### 大小寫
>func ToLower(s string) string
字符全部轉換為小寫
> func ToUpper(s string) string
字符全部轉換為大寫
```
fmt.Println(strings.ToLower("GO")) //go
fmt.Println(strings.ToUpper("go")) //GO
```
### 標題處理
>func Title(s string) string
首字母大寫
>func ToTitle(s string) string
將每個字母大寫
>func ToTitleSpecial(c unicode.SpecialCase, s string) string
每個字母大寫,并且會將一些特殊字母轉換為其對應的特殊大寫字母
```
title := "hello world"
fmt.Println(strings.Title(title)) //Hello World
fmt.Println(strings.ToTitle(title)) //HELLO WORLD
```
### 字符串替換
>func Replace(s, old, new string, n int) string
用 new 替換 s 中的 old,一共替換 n 個。
如果 n < 0,則不限制替換次數,即全部替換
>func ReplaceAll(s, old, new string) string
該函數內部直接調用了函數 Replace(s, old, new , -1)
```
s := "book book book"
fmt.Println(strings.Replace(s,"o", "l",1))//blok book book
fmt.Println(strings.Replace(s,"o", "l",2))//bllk book book
fmt.Println(strings.ReplaceAll(s,"o", "l"))//bllk bllk bllk
```
### 重復字符串
> func Repeat(s string, count int) string
將字符串重復count次
```
s := "book"
fmt.Println(strings.Repeat(s,2))//bookbook
```
### 裁剪字符串
> func Trim(s string, cutset string) string
將 s 左右兩側中匹配 cutset 中的任一字符的字符去掉
```
s := "dhello world"
s1 := strings.Trim(s,"d")
fmt.Printf("%s",s1)//hello worl
```
>func TrimLeft(s string, cutset string) string
將 s 左側的匹配 cutset 中的任一字符的字符去掉
```
s := "dhello world"
s1 := strings.TrimLeft(s,"d")
fmt.Printf("%s",s1)//hello world
```
>func TrimRight(s string, cutset string) string
將 s 右側的匹配 cutset 中的任一字符的字符去掉
```
s := "dhello world"
s1 := strings.TrimRight(s,"d")
fmt.Printf("%s",s1)//dhello worl
```
>func TrimPrefix(s, prefix string) string
如果 s 的前綴為 prefix 則返回去掉前綴后的 string , 否則 s 沒有變化。
```
s := "dhello world"
s1 := strings.TrimPrefix(s,"d")
fmt.Printf("%s",s1)//hello world
```
>func TrimSuffix(s, suffix string) string
如果 s 的后綴為 suffix 則返回去掉后綴后的 string , 否則 s 沒有變化。
```
s := "dhello world"
s1 := strings.TrimSuffix(s,"d")
fmt.Printf("%s",s1)//dhello worl
```
>func TrimSpace(s string) string
將 s 左右兩側的間隔符去掉。常見間隔符包括:'\t', '\n', '\v', '\f', '\r', ' '
```
s := " dhello world "
s1 := strings.Trim(s," ")
s2 := strings.TrimSpace(s)
fmt.Printf("%s %v \n",s1,strings.Split(s1," "))//dhello world [dhello world]
fmt.Printf("%s %v \n",s2,strings.Split(s2," "))//dhello world [dhello world]
Trim 也可以達到TrimSpace的效果
```
## strconv
### Atoi
```
i,_ := strconv.Atoi("100")
fmt.Printf("%d %T",i,i)//100 int
```
### Itoa
```
s:= strconv.Itoa(100)
fmt.Printf("%s %T",s,s)//"100" string
```
### 字符串轉其他類型 Parse系列函數
#### ParseBool
>func ParseBool(str string) (value bool, err error)
返回字符串表示的bool值。它接受1、0、t、f、T、F、true、false、True、False、TRUE、FALSE;否則返回錯誤。
```
b,_ := strconv.ParseBool("1")
fmt.Printf("%T %t\n",b,b)//bool true
```
#### ParseInt
>func ParseInt(s string, base int, bitSize int) (i int64, err error)
base指定進制(2到36),如果base為0,則會從字符串前置判斷,“0x"是16進制,“0"是8進制,否則是10進制;
bitSize指定結果必須能無溢出賦值的整數類型,0、8、16、32、64 分別代表 int、int8、int16、int32、int64;
```
i, _ := strconv.ParseInt("-2", 10, 64)
fmt.Printf("%T %d\n",i,i)//int64 -2
```
#### ParseFloat
>func ParseFloat(s string, bitSize int) (f float64, err error)
bitSize指定了期望的接收類型,32是float32(返回值可以不改變精確值的賦值給float32),64是float64;
返回值err是*NumErr類型的,語法有誤的,err.Error=ErrSyntax;結果超出表示范圍的,返回值f為±Inf,err.Error= ErrRange。
```
f, _ := strconv.ParseFloat("3.1415", 32)
fmt.Printf("%T\n",f)//float64
```
#### ParseUint
>func ParseUint(s string, base int, bitSize int) (n uint64, err error)
```
u, _ := strconv.ParseUint("2", 10, 64)
fmt.Printf("%T %d\n",u,u)//uint64 2
```
### 其他類型轉字符串 Format系列函數
#### FormatBool
>func FormatBool(b bool) string
```
b1 := true
b2 := false
fmt.Printf("%s %T \n",strconv.FormatBool(b1),strconv.FormatBool(b1))//true string
fmt.Printf("%s %T \n",strconv.FormatBool(b2),strconv.FormatBool(b2))//false string
```
#### FormatInt
>func FormatInt(i int64, base int) string
返回i的base進制的字符串表示。base 必須在2到36之間,結果中會使用小寫字母’a’到’z’表示大于10的數字。
```
i := int64(-100)
fmt.Printf("%s %T \n",strconv.FormatInt(i,2),strconv.FormatInt(i,2))//-1100100 string
```
#### FormatFloat
>func FormatFloat(f float64, fmt byte, prec, bitSize int) string
* fmt表示格式:‘f’(-ddd.dddd)、‘b’(-ddddp±ddd,指數為二進制)、’e’(-d.dddde±dd,十進制指數)、‘E’(-d.ddddE±dd,十進制指數)、‘g’(指數很大時用’e’格式,否則’f’格式)、‘G’(指數很大時用’E’格式,否則’f’格式)。
* prec控制精度(排除指數部分):對’f’、’e’、‘E’,它表示小數點后的數字個數;對’g’、‘G’,它控制總的數字個數。如果prec 為-1,則代表使用最少數量的、但又必需的數字來表示f。
* bitSize表示f的來源類型(32:float32、64:float64),會據此進行舍入。
```
s := strconv.FormatFloat(1.732, 'E', -1, 64)
fmt.Printf("%s %T \n",s,s) //1.732E+00 string
```
#### FormatUint
>func FormatUint(i uint64, base int) string
```
i := int64(100)
fmt.Printf("%s %T \n",strconv.FormatUint(i,2),strconv.FormatUint(i,2))//1100100 string
```
- Go準備工作
- 依賴管理
- Go基礎
- 1、變量和常量
- 2、基本數據類型
- 3、運算符
- 4、流程控制
- 5、數組
- 數組聲明和初始化
- 遍歷
- 數組是值類型
- 6、切片
- 定義
- slice其他內容
- 7、map
- 8、函數
- 函數基礎
- 函數進階
- 9、指針
- 10、結構體
- 類型別名和自定義類型
- 結構體
- 11、接口
- 12、反射
- 13、并發
- 14、網絡編程
- 15、單元測試
- Go常用庫/包
- Context
- time
- strings/strconv
- file
- http
- Go常用第三方包
- Go優化
- Go問題排查
- Go框架
- 基礎知識點的思考
- 面試題
- 八股文
- 操作系統
- 整理一份資料
- interface
- array
- slice
- map
- MUTEX
- RWMUTEX
- Channel
- waitGroup
- context
- reflect
- gc
- GMP和CSP
- Select
- Docker
- 基本命令
- dockerfile
- docker-compose
- rpc和grpc
- consul和etcd
- ETCD
- consul
- gin
- 一些小點
- 樹
- K8s
- ES
- pprof
- mycat
- nginx
- 整理后的面試題
- 基礎
- Map
- Chan
- GC
- GMP
- 并發
- 內存
- 算法
- docker