golang学习笔记——字符串操作


字符串操作 package main   import (     "fmt"     "strings" )   func main() {     str := "hello world"     //contains 是否包含指定字符串     fmt.Println(strings.Contains(str, "hello"))     //Jion 将数组或者切片转为字符串     str1 := []string{"hello", "world"}     fmt.Println(strings.Join(str1, "."))     //index 找出指定字符第一次出现的位置     fmt.Println(strings.Index("elloh", "h"))     //repeat 重复指定字符指定次数     fmt.Println(strings.Repeat("ha", 6))     //split 以指定分隔符拆分字符串     fmt.Println(strings.Split("hello,world", ","))     //trim 去除首尾指定字符串     fmt.Println(strings.Trim("qqhelloworldqq", "qq"))     //fields 以空格分割拆分字符串     fmt.Println(strings.Fields("hello world")) } 字符串转化 package main   import (     "fmt"     "strconv" )   func main() {     slice := make([]byte, 0, 1024)     //boole值转为字符串并追加     slice = strconv.AppendBool(slice, true)     //整形转为字符串并追加,第三个参数表示十进制     slice = strconv.AppendInt(slice, 12345, 10)     //追加字符串     slice = strconv.AppendQuote(slice, "hello")     fmt.Println(string(slice))       //其他类型转为字符串     fmt.Println(strconv.FormatBool(true))     fmt.Println(strconv.FormatInt(23459, 10))       //字符串转其他类型     str := "true"     b, err := strconv.ParseBool(str)     if err == nil {          fmt.Println(b)     }       //字符串转为整形     i, _ := strconv.Atoi("123")     fmt.Println(i)       //整形转为字符串     strconv.Itoa(123)   }