Golang strconv包使用
摘要:// atob.go
------------------------------------------------------------
// ParseBool 將字串轉換為布林值
// 它接受真值:1, t, T, TRUE, true, T...
// atob.go ------------------------------------------------------------ // ParseBool 將字串轉換為布林值 // 它接受真值:1, t, T, TRUE, true, True // 它接受假值:0, f, F, FALSE, false, False. // 其它任何值都返回一個錯誤 func ParseBool(str string) (value bool, err error) func main() { fmt.Println(strconv.ParseBool("1"))// true fmt.Println(strconv.ParseBool("t"))// true fmt.Println(strconv.ParseBool("T"))// true fmt.Println(strconv.ParseBool("true")) // true fmt.Println(strconv.ParseBool("True")) // true fmt.Println(strconv.ParseBool("TRUE")) // true fmt.Println(strconv.ParseBool("TRue"))// false strconv.ParseBool: parsing "TRue": invalid syntax fmt.Println(strconv.ParseBool("0"))// false fmt.Println(strconv.ParseBool("f"))// false fmt.Println(strconv.ParseBool("F"))// false fmt.Println(strconv.ParseBool("false")) // false fmt.Println(strconv.ParseBool("False")) // false fmt.Println(strconv.ParseBool("FALSE")) // false fmt.Println(strconv.ParseBool("FALse")) // false strconv.ParseBool: parsing "FAlse": invalid syntax } ------------------------------------------------------------ // FormatBool 將布林值轉換為字串 "true" 或 "false" func FormatBool(b bool) string func main() { fmt.Println(strconv.FormatBool(0 < 1)) // true fmt.Println(strconv.FormatBool(0 > 1)) // false } ------------------------------------------------------------ // AppendBool 將布林值 b 轉換為字串 "true" 或 "false" // 然後將結果追加到 dst 的尾部,返回追加後的 []bytefunc AppendBool(dst []byte, b bool) []byte func main() { rst := make([]byte, 0) rst = strconv.AppendBool(rst, 0 < 1) fmt.Printf("%s\n", rst) // true rst = strconv.AppendBool(rst, 0 > 1) fmt.Printf("%s\n", rst) // truefalse } ============================================================ // atof.go ------------------------------------------------------------ // ParseFloat 將字串轉換為浮點數 // s:要轉換的字串 // bitSize:指定浮點型別(32:float32、64:float64) // 如果 s 是合法的格式,而且接近一個浮點值, // 則返回浮點數的四捨五入值(依據 IEEE754 的四捨五入標準) // 如果 s 不是合法的格式,則返回“語法錯誤” // 如果轉換結果超出 bitSize 範圍,則返回“超出範圍” func ParseFloat(s string, bitSize int) (f float64, err error) func main() { s := "0.12345678901234567890" f, err := strconv.ParseFloat(s, 32) fmt.Println(f, err)// 0.12345679104328156 fmt.Println(float32(f), err)// 0.12345679 f, err = strconv.ParseFloat(s, 64) fmt.Println(f, err)// 0.12345678901234568 } ============================================================ // atoi.go ------------------------------------------------------------ // ErrRange 表示值超出範圍var ErrRange = errors.New("value out of range") // ErrSyntax 表示語法不正確var ErrSyntax = errors.New("invalid syntax") // NumError 記錄轉換失敗 type NumError struct { Func string // 失敗的函式名(ParseBool, ParseInt, ParseUint, ParseFloat) Numstring // 輸入的值 Errerror// 失敗的原因(ErrRange, ErrSyntax) } // int 或 uint 型別的長度(32 或 64) const IntSize = intSize const intSize = 32 << uint(^uint(0)>>63) // 實現 Error 介面,輸出錯誤資訊 func (e *NumError) Error() string ------------------------------------------------------------ // ParseInt 將字串轉換為 int 型別 // s:要轉換的字串 // base:進位制(2 進位制到 36 進位制) // bitSize:指定整數型別(0:int、8:int8、16:int16、32:int32、64:int64) // 返回轉換後的結果和轉換時遇到的錯誤 // 如果 base 為 0,則根據字串的字首判斷進位制(0x:16,0:8,其它:10) func ParseInt(s string, base int, bitSize int) (i int64, err error) func main() { fmt.Println(strconv.ParseInt("123", 10, 8)) // 123 fmt.Println(strconv.ParseInt("12345", 10, 8))// 127 strconv.ParseInt: parsing "12345": value out of range fmt.Println(strconv.ParseInt("2147483647", 10, 0))// 2147483647 fmt.Println(strconv.ParseInt("0xFF", 16, 0))// 0 strconv.ParseInt: parsing "0xFF": invalid syntax fmt.Println(strconv.ParseInt("FF", 16, 0))// 255 fmt.Println(strconv.ParseInt("0xFF", 0, 0)) // 255 } ------------------------------------------------------------ // ParseUint 功能同 ParseInt 一樣,只不過返回 uint 型別整數 func ParseUint(s string, base int, bitSize int) (n uint64, err error) func main() { fmt.Println(strconv.ParseUint("FF", 16, 8)) // 255 } ------------------------------------------------------------ // Atoi 相當於 ParseInt(s, 10, 0) // 通常使用這個函式,而不使用 ParseIntfunc Atoi(s string) (i int, err error) func main() { fmt.Println(strconv.Atoi("2147483647")) // 2147483647 fmt.Println(strconv.Atoi("2147483648")) // 2147483647 strconv.ParseInt: parsing "2147483648": value out of range } ============================================================ // ftoa.go ------------------------------------------------------------ // FormatFloat 將浮點數 f 轉換為字串值 // f:要轉換的浮點數 // fmt:格式標記(b、e、E、f、g、G) // prec:精度(數字部分的長度,不包括指數部分) // bitSize:指定浮點型別(32:float32、64:float64) //// 格式標記: // 'b' (-ddddp±ddd,二進位制指數) // 'e' (-d.dddde±dd,十進位制指數) // 'E' (-d.ddddE±dd,十進位制指數) // 'f' (-ddd.dddd,沒有指數) // 'g' ('e':大指數,'f':其它情況) // 'G' ('E':大指數,'f':其它情況) //// 如果格式標記為 'e','E'和'f',則 prec 表示小數點後的數字位數 // 如果格式標記為 'g','G',則 prec 表示總的數字位數(整數部分+小數部分) func FormatFloat(f float64, fmt byte, prec, bitSize int) string func main() { f := 100.12345678901234567890123456789 fmt.Println(strconv.FormatFloat(f, 'b', 5, 32)) // 13123382p-17 fmt.Println(strconv.FormatFloat(f, 'e', 5, 32)) // 1.00123e+02 fmt.Println(strconv.FormatFloat(f, 'E', 5, 32)) // 1.00123E+02 fmt.Println(strconv.FormatFloat(f, 'f', 5, 32)) // 100.12346 fmt.Println(strconv.FormatFloat(f, 'g', 5, 32)) // 100.12 fmt.Println(strconv.FormatFloat(f, 'G', 5, 32)) // 100.12 fmt.Println(strconv.FormatFloat(f, 'b', 30, 32))// 13123382p-17 fmt.Println(strconv.FormatFloat(f, 'e', 30, 32))// 1.001234588623046875000000000000e+02 fmt.Println(strconv.FormatFloat(f, 'E', 30, 32))// 1.001234588623046875000000000000E+02 fmt.Println(strconv.FormatFloat(f, 'f', 30, 32))// 100.123458862304687500000000000000 fmt.Println(strconv.FormatFloat(f, 'g', 30, 32))// 100.1234588623046875 fmt.Println(strconv.FormatFloat(f, 'G', 30, 32))// 100.1234588623046875 } ------------------------------------------------------------ // AppendFloat 將浮點數 f 轉換為字串值,並將轉換結果追加到 dst 的尾部 // 返回追加後的 []bytefunc AppendFloat(dst []byte, f float64, fmt byte, prec int, bitSize int) []byte func main() { f := 100.12345678901234567890123456789 b := make([]byte, 0) b = strconv.AppendFloat(b, f, 'f', 5, 32) b = append(b, ""...) b = strconv.AppendFloat(b, f, 'e', 5, 32) fmt.Printf("%s", b) // 100.123461.00123e+0 } ============================================================ // itoa.go ------------------------------------------------------------ // FormatUint 將 int 型整數 i 轉換為字串形式 // base:進位制(2 進位制到 36 進位制) // 大於 10 進位制的數,返回值使用小寫字母 'a' 到 'z'func FormatInt(i int64, base int) string func main() { i := int64(-2048) fmt.Println(strconv.FormatInt(i, 2))// -100000000000 fmt.Println(strconv.FormatInt(i, 8))// -4000 fmt.Println(strconv.FormatInt(i, 10)) // -2048 fmt.Println(strconv.FormatInt(i, 16)) // -800 fmt.Println(strconv.FormatInt(i, 36)) // -1kw } ------------------------------------------------------------ // FormatUint 將 uint 型整數 i 轉換為字串形式 // base:進位制(2 進位制到 36 進位制) // 大於 10 進位制的數,返回值使用小寫字母 'a' 到 'z'func FormatUint(i uint64, base int) string func main() { i := uint64(2048) fmt.Println(strconv.FormatUint(i, 2))// 100000000000 fmt.Println(strconv.FormatUint(i, 8))// 4000 fmt.Println(strconv.FormatUint(i, 10)) // 2048 fmt.Println(strconv.FormatUint(i, 16)) // 800 fmt.Println(strconv.FormatUint(i, 36)) // 1kw } ------------------------------------------------------------ // Itoa 相當於 FormatInt(i, 10)func Itoa(i int) string func main() { fmt.Println(strconv.Itoa(-2048)) // -2048 fmt.Println(strconv.Itoa(2048))// 2048 } ------------------------------------------------------------ // AppendInt 將 int 型整數 i 轉換為字串形式,並追加到 dst 的尾部 // i:要轉換的字串 // base:進位制 // 返回追加後的 []bytefunc AppendInt(dst []byte, i int64, base int) []byte func main() { b := make([]byte, 0) b = strconv.AppendInt(b, -2048, 16) fmt.Printf("%s", b) // -800 } ------------------------------------------------------------ // AppendUint 將 uint 型整數 i 轉換為字串形式,並追加到 dst 的尾部 // i:要轉換的字串 // base:進位制 // 返回追加後的 []bytefunc AppendUint(dst []byte, i uint64, base int) []byte func main() { b := make([]byte, 0) b = strconv.AppendUint(b, 2048, 16) fmt.Printf("%s", b) // 800 } ============================================================ // quote.go ------------------------------------------------------------ // Quote 將字串 s 轉換為“雙引號”引起來的字串 // 其中的特殊字元將被轉換為“轉義字元” // “不可顯示的字元”將被轉換為“轉義字元” func Quote(s string) string func main() { fmt.Println(strconv.Quote(`C:\Windows`))// "C:\\Windows" } ------------------------------------------------------------ // AppendQuote 將字串 s 轉換為“雙引號”引起來的字串, // 並將結果追加到 dst 的尾部,返回追加後的 []byte // 其中的特殊字元將被轉換為“轉義字元”func AppendQuote(dst []byte, s string) []byte func main() { s := `C:\Windows` b := make([]byte, 0) b = strconv.AppendQuote(b, s) fmt.Printf("%s", b) // "C:\\Windows" } ------------------------------------------------------------ // QuoteToASCII 將字串 s 轉換為“雙引號”引起來的 ASCII 字串 // “非 ASCII 字元”和“特殊字元”將被轉換為“轉義字元” func QuoteToASCII(s string) string func main() { fmt.Println(strconv.QuoteToASCII("Hello 世界!"))// "Hello \u4e16\u754c\uff01" } ------------------------------------------------------------ // AppendQuoteToASCII 將字串 s 轉換為“雙引號”引起來的 ASCII 字串, // 並將結果追加到 dst 的尾部,返回追加後的 []byte // “非 ASCII 字元”和“特殊字元”將被轉換為“轉義字元” func AppendQuoteToASCII(dst []byte, s string) []byte func main() { s := "Hello 世界!" b := make([]byte, 0) b = strconv.AppendQuoteToASCII(b, s) fmt.Printf("%s", b) // "Hello \u4e16\u754c\uff01" } ------------------------------------------------------------ // QuoteRune 將 Unicode 字元轉換為“單引號”引起來的字串 // “特殊字元”將被轉換為“轉義字元” func QuoteRune(r rune) string func main() { fmt.Println(strconv.QuoteRune('好')) // '好' } ------------------------------------------------------------ // AppendQuoteRune 將 Unicode 字元轉換為“單引號”引起來的字串, // 並將結果追加到 dst 的尾部,返回追加後的 []byte // “特殊字元”將被轉換為“轉義字元” func AppendQuoteRune(dst []byte, r rune) []byte func main() { b := make([]byte, 0) b = strconv.AppendQuoteRune(b, '好') fmt.Printf("%s", b) // '好' } ------------------------------------------------------------ // QuoteRuneToASCII 將 Unicode 字元轉換為“單引號”引起來的 ASCII 字串 // “非 ASCII 字元”和“特殊字元”將被轉換為“轉義字元” func QuoteRuneToASCII(r rune) string func main() { fmt.Println(strconv.QuoteRuneToASCII('好'))// '\u597d' } ------------------------------------------------------------ // AppendQuoteRune 將 Unicode 字元轉換為“單引號”引起來的 ASCII 字串, // 並將結果追加到 dst 的尾部,返回追加後的 []byte // “非 ASCII 字元”和“特殊字元”將被轉換為“轉義字元” func AppendQuoteRuneToASCII(dst []byte, r rune) []byte func main() { b := make([]byte, 0) b = strconv.AppendQuoteRuneToASCII(b, '好') fmt.Printf("%s", b) // '\u597d' } ------------------------------------------------------------ // CanBackquote 判斷字串 s 是否可以表示為一個單行的“反引號”字串 // 字串中不能含有控制字元(除了 \t)和“反引號”字元,否則返回 false func CanBackquote(s string) bool func main() { b := strconv.CanBackquote("C:\\Windows\n") fmt.Println(b) // false b = strconv.CanBackquote("C:\\Windows\r") fmt.Println(b) // false b = strconv.CanBackquote("C:\\Windows\f") fmt.Println(b) // false b = strconv.CanBackquote("C:\\Windows\t") fmt.Println(b) // true b = strconv.CanBackquote("C:\\`Windows`") fmt.Println(b) // false } ------------------------------------------------------------ // UnquoteChar 將 s 中的第一個字元“取消轉義”並解碼 //// s:轉義後的字串 // quote:字串使用的“引號符”(用於對引號符“取消轉義”) //// value:解碼後的字元 // multibyte:value 是否為多位元組字元 // tail:字串 s 除去 value 後的剩餘部分 // error:返回 s 中是否存在語法錯誤 //// 引數 quote 為“引號符” // 如果設定為單引號,則 s 中允許出現 \' 字元,不允許出現單獨的 ' 字元 // 如果設定為雙引號,則 s 中允許出現 \" 字元,不允許出現單獨的 " 字元 // 如果設定為 0,則不允許出現 \' 或 \" 字元,可以出現單獨的 ' 或 " 字元 func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error) func main() { s := `\"大\\家\\好!\"` c, mb, sr, _ := strconv.UnquoteChar(s, '"') fmt.Printf("%-3c%v\n", c, mb) for ; len(sr) > 0; c, mb, sr, _ = strconv.UnquoteChar(sr, '"') { fmt.Printf("%-3c%v\n", c, mb) }// "false // 大true // \false // 家true // \false // 好true // !true } ------------------------------------------------------------ // Unquote 將“帶引號的字串” s 轉換為常規的字串(不帶引號和轉義字元) // s 可以是“單引號”、“雙引號”或“反引號”引起來的字串(包括引號本身) // 如果 s 是單引號引起來的字串,則返回該該字串代表的字元 func Unquote(s string) (t string, err error) func main() { sr, err := strconv.Unquote(`"\"大\t家\t好!\""`) fmt.Println(sr, err) sr, err = strconv.Unquote(`'大家好!'`) fmt.Println(sr, err) sr, err = strconv.Unquote(`'好'`) fmt.Println(sr, err) sr, err = strconv.Unquote("`大\\t家\\t好!`") fmt.Println(sr, err) } ------------------------------------------------------------ // IsPrint 判斷 Unicode 字元 r 是否是一個可顯示的字元 // 可否顯示並不是你想象的那樣,比如空格可以顯示,而\t則不能顯示 // 具體可以參考 Go 語言的原始碼 func IsPrint(r rune) bool func main() { fmt.Println(strconv.IsPrint('a'))// true fmt.Println(strconv.IsPrint('好'))// true fmt.Println(strconv.IsPrint(' '))// true fmt.Println(strconv.IsPrint('\t'))// false fmt.Println(strconv.IsPrint('\n'))// false fmt.Println(strconv.IsPrint(0))// false }