Go/文字處理/字串處理
阿新 • • 發佈:2018-12-22
# 字串常用函式
package main import ( "fmt" "strconv" "strings" ) func main() { //是否包含子串 fmt.Println(strings.Contains("hellogo","go")) //true //切片連成字串 s := []string{"aa","bb","cc"} fmt.Println(strings.Join(s,"@")) //[email protected]@cc //查詢子串位置 fmt.Println(strings.Index("hellogo","go")) //5 fmt.Println(strings.Index("hellogo","goo")) //-1 //重複出現 fmt.Println(strings.Repeat("go",3)) //gogogo //分隔 ss := strings.Split("aa,bb,cc",",") fmt.Println(ss) //[aa bb cc] //去掉兩頭指定字串 fmt.Println(strings.Trim(" aa bb "," ")) //aa bb //按空格分割 ss = strings.Fields(" aa bb cc ") fmt.Println(ss) test() } func test(){ slice := make([]byte,0,1024) slice = strconv.AppendBool(slice,true) slice = strconv.AppendInt(slice,314,10) //十進位制方式 slice = strconv.AppendQuote(slice,"aaaaaa") fmt.Println(string(slice)) //true314"aaaaaa" //其它型別轉化為字串 str := strconv.FormatInt(100,16) fmt.Println(str) //64 16進位制 fmt.Println(strconv.Itoa(999)) str = strconv.FormatFloat(3.1415,'f',-1,64) fmt.Println(str) //字串轉化為其它型別 flg,_ := strconv.ParseBool("true") fmt.Printf("%T %v \n",flg,flg) a,err := strconv.Atoi("100") fmt.Println(a,err) }