Go語言中其他資料與字串型別的轉換
1 概述
Go語言是強型別語言,因此總會需要將字串轉成需要的型別。比如整型和字串轉換,字串和布林型的轉換等。本文就介紹如何完成這些轉換,以下是Go語言關於字串轉換的整理說明,主要是與切片型別的轉換,和 strconv 包的使用。
2 與切片的轉換
切片型別可以和字串型別相互轉換。
fmt.Println([]rune("Hello小韓說課")) // [72 101 108 108 111 23567 38889 35828 35838] fmt.Println(string([]rune{72, 101, 108, 108, 111, 23567, 38889, 35828, 35838})) // Hello小韓說課 fmt.Println([]byte("Hello")) // [72 101 108 108 111] fmt.Println(string([]byte{72, 101, 108, 108, 111})) // Hello
3 strconv 包
會將常用的放在前面:
strconv.Atoi(s string) (int, error)
轉換字串 s string 到整型。
v := "10" if s, err := strconv.Atoi(v); err == nil { fmt.Printf("%T, %v", s, s) } // int, 10 // 相當於 strconv.ParseInt(s, 10, 0)
strconv.Itoa(i int) string
將整型轉 i int 換為字串
i := 10 s := strconv.Itoa(i) fmt.Printf("%T, %v\n", s, s) // string, 10
strconv.ParseFloat(s string, bitSize int) (float64, error)
解析字元 str string 串為浮點,可以設定位數。
v := "3.1415926535" if s, err := strconv.ParseFloat(v, 32); err == nil { fmt.Printf("%T, %v\n", s, s) } // float64, 3.1415927410125732 if s, err := strconv.ParseFloat(v, 64); err == nil { fmt.Printf("%T, %v\n", s, s) } // float64, 3.1415926535
strconv.ParseInt(s string, base int, bitSize int) (i int64, err error)
解析字元 str string 串為整數,可以設定進位制、位數。
v32 := "-354634382" if s, err := strconv.ParseInt(v32, 10, 32); err == nil { fmt.Printf("%T, %v\n", s, s) } if s, err := strconv.ParseInt(v32, 16, 32); err == nil { fmt.Printf("%T, %v\n", s, s) } // int64, -354634382 v64 := "-3546343826724305832" if s, err := strconv.ParseInt(v64, 10, 64); err == nil { fmt.Printf("%T, %v\n", s, s) } if s, err := strconv.ParseInt(v64, 16, 64); err == nil { fmt.Printf("%T, %v\n", s, s) } // int64, -3546343826724305832
strconv.FormatFloat(f float64, fmt byte, prec, bitSize int) string
將浮點數 f float64 轉換為字串,可以設定格式 fmt(b,e,E,f,g,G)、精度prce、位數(32或64)。
v := 3.1415926535 s32 := strconv.FormatFloat(v, 'E', -1, 32) fmt.Printf("%T, %v\n", s32, s32) // string, 3.1415927E+00 s64 := strconv.FormatFloat(v, 'E', -1, 64) fmt.Printf("%T, %v\n", s64, s64) // string, 3.1415926535E+00
strconv.FormatInt(i int64, base int) string
將整數 i int64 轉換為字串,可以指定進位制 base。
v := int64(-42) s10 := strconv.FormatInt(v, 10) fmt.Printf("%T, %v\n", s10, s10) // string, -42 s16 := strconv.FormatInt(v, 16) fmt.Printf("%T, %v\n", s16, s16) // string, -2a
strconv.AppendBool(dst []byte, b bool) []byte
將布林值 b bool 以字串形式追加到 dst []byte 中。
b := []byte("bool:") b = strconv.AppendBool(b, true) fmt.Println(string(b)) // bool:true // 相當於 append(dst []byte, strconv.FormatBool(b bool))
strconv.AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte
將浮點數 f float64 以字串形式追加到 dst []byte 中,可以設定格式 fmt(b,e,E,f,g,G)、精度prce、位數(32或64)。
b32 := []byte("float32:") b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32) fmt.Println(string(b32)) // float32:3.1415927E+00 b64 := []byte("float64:") b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64) fmt.Println(string(b64)) // float64:3.1415926535E+00 // 相當於 append(dst []byte, strconv.FormatFloat(3.1415926535, 'E', -1, 64))
strconv.AppendInt(dst []byte, i int64, base int) []byte
將整數 i int64 以字串形式追加到 dst []byte 中,可以指定進位制。
b10 := []byte("int (base 10):") b10 = strconv.AppendInt(b10, -42, 10) fmt.Println(string(b10)) // int (base 10):-42 b16 := []byte("int (base 16):") b16 = strconv.AppendInt(b16, -42, 16) fmt.Println(string(b16)) // int (base 16):-2a // 相當於 append(dst []byte, strconv.FormatInt(-42, 10)) append(dst []byte, strconv.FormatInt(-42, 16))
strconv.AppendQuote(dst []byte, s string) []byte
將字串 s string 以字串雙引號定義的形式追加到 dst []byte 中。
b := []byte("quote:") b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`) fmt.Println(string(b)) // quote:"\"Fran & Freddie's Diner\"" // 相當於 append(dst []byte, strconv.Quote(`"Fran & Freddie's Diner"`))
strconv.AppendQuoteRune(dst []byte, r rune) []byte
將字元 r rune 以字單引號定義的形式追加到 dst []byte 中。
b := []byte("rune:") b = strconv.AppendQuoteRune(b, '☺') fmt.Println(string(b)) // rune:'☺' // 相當於 append(dst []byte, strconv.QuoteRune('☺'))
strconv.AppendQuoteRuneToASCII(dst []byte, r rune) []byte
將字元 r rune 以字單引號定義的形式追加到 dst []byte 中,對於非 ASCII 字元 r 會以轉義字元的形式出現。
b := []byte("rune (ascii):") b = strconv.AppendQuoteRuneToASCII(b, '☺') fmt.Println(string(b)) // rune (ascii):'\u263a' // 相當於 append(dst []byte, strconv.QuoteRuneToASCII('☺'))
strconv.AppendQuoteToASCII(dst []byte, s string) []byte
將字串 s string 以字串雙引號定義的形式追加到 dst []byte 中,非 ASCII 字元以轉義形式表示。
b := []byte("quote (ascii):") b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`) fmt.Println(string(b)) // quote (ascii):"\"Fran & Freddie's Diner\"" // 相當於 append(dst []byte, strconv.QuoteToASCII(`"Fran & Freddie's Diner"`))
strconv.AppendUint(dst []byte, i uint64, base int) []byte
將無符號整數 i uint64 以字串形式追加到 dst []byte 中,可以指定進位制。
b10 := []byte("uint (base 10):") b10 = strconv.AppendUint(b10, 42, 10) fmt.Println(string(b10)) // uint (base 10):42 b16 := []byte("uint (base 16):") b16 = strconv.AppendUint(b16, 42, 16) fmt.Println(string(b16)) // uint (base 16):2a
strconv.CanBackquote(s string) bool
檢測字串 s string 是否可以不被修改的表示為一個單行的、沒有空格和tab之外控制字元的反引號字串。
fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺")) // true fmt.Println(strconv.CanBackquote("`can't backquote this`")) // false
strconv.FormatBool(b bool) string
將布林 b bool 轉換為字串。
v := true s := strconv.FormatBool(v) fmt.Printf("%T, %v\n", s, s) // string, true
strconv.FormatUint(i uint64, base int) string
將無符號整數 i uint64 轉換為字串,可以指定進位制 base。
v := uint64(42) s10 := strconv.FormatUint(v, 10) fmt.Printf("%T, %v\n", s10, s10) // string, 42 s16 := strconv.FormatUint(v, 16) fmt.Printf("%T, %v\n", s16, s16) // string, 2a
strconv.IsPrint(r rune) bool
檢測字元 r rune 是否為列印字元。
c := strconv.IsPrint('\u263a') fmt.Println(c) // true bel := strconv.IsPrint('\007') fmt.Println(bel) // false
strconv.ParseBool(str string) (bool, error)
解析字元 str string 串為布林型。
v := "true" if s, err := strconv.ParseBool(v); err == nil { fmt.Printf("%T, %v\n", s, s) } // bool, true
strconv.ParseUint(s string, base int, bitSize int) (uint64, error)
解析字元 str string 串為無符號整數,可以設定進位制、位數。
v := "42" if s, err := strconv.ParseUint(v, 10, 32); err == nil { fmt.Printf("%T, %v\n", s, s) } // uint64, 42 if s, err := strconv.ParseUint(v, 10, 64); err == nil { fmt.Printf("%T, %v\n", s, s) } // uint64, 42
strconv.Quote(s string) string
返回字串 s string 雙引號字面值表示,控制字元、不可列印字元會進行轉義,如 \t,\n,\xFF,\u0100。
s := strconv.Quote(`"Fran & Freddie's Diner ☺"`) fmt.Println(s) // "\"Fran & Freddie's Diner\t☺\""
strconv.QuoteRune(r rune) string
返回字元 r rune 單引號字面值表示,控制字元、不可列印字元會進行轉義,如\t,\n,\xFF,\u0100。
s := strconv.QuoteRune('☺') fmt.Println(s) // '☺'
strconv.QuoteRuneToASCII(r rune) string
返回字元 r rune 單引號字面值表示,控制字元、不可列印字元、非ASCII字元會進行轉義。
s := strconv.QuoteRuneToASCII('☺') fmt.Println(s) // '\u263a'
strconv.QuoteToASCII(s string) string
返回字串 s string 雙引號字面值表示,控制字元和不可列印字元、非ASCII字元會進行轉義。
s := strconv.QuoteToASCII(`"Fran & Freddie's Diner☺"`) fmt.Println(s) // "\"Fran & Freddie's Diner\t\u263a\""
strconv.Unquote(s string) (string, error)
返回一個單引號、雙引號、反引號包圍的語法字串 s string,解析它並返回它表示的值。若為反引號括起來的,函式會認為s是go字元字面值,返回一個單字元的字串。
s, err := strconv.Unquote("You can't unquote a string without quotes") fmt.Printf("%q, %v\n", s, err) // "", invalid syntax s, err = strconv.Unquote("\"The string must be either double-quoted\"") fmt.Printf("%q, %v\n", s, err) // "The string must be either double-quoted", <nil> s, err = strconv.Unquote("`or backquoted.`") fmt.Printf("%q, %v\n", s, err) // "or backquoted.", <nil> s, err = strconv.Unquote("'\u263a'") // single character only allowed in single quotes fmt.Printf("%q, %v\n", s, err) // "☺", <nil> s, err = strconv.Unquote("'\u2639\u2639'") fmt.Printf("%q, %v\n", s, err) // "", invalid syntax
strconv.UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
返回一個表示字元的語法字串 s string,可以設定字串定義語法 quote byte 雙引號或者反引號。解析它並返回四個值:
v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"') if err != nil { log.Fatal(err) } fmt.Println("value:", string(v)) // value: " fmt.Println("multibyte:", mb) // multibyte: false fmt.Println("tail:", t) // tail: Fran & Freddie's Diner\"