strconv

最后更新于:2022-04-02 02:46:00

[TOC] ## 语法 ``` func AppendBool(dst []byte, b bool) []byte // 把 布尔值追加到字符串 func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte func AppendInt(dst []byte, i int64, base int) []byte func AppendQuote(dst []byte, s string) []byte func AppendQuoteRune(dst []byte, r rune) []byte func AppendQuoteRuneToASCII(dst []byte, r rune) []byte func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte func AppendQuoteToASCII(dst []byte, s string) []byte func AppendQuoteToGraphic(dst []byte, s string) []byte func AppendUint(dst []byte, i uint64, base int) []byte // 字符串转整数 func Atoi(s string) (int, error) func Itoa(i int) string func CanBackquote(s string) bool // 布尔转字符串,如:true转为 "true" func FormatBool(b bool) string func FormatComplex(c complex128, fmt byte, prec, bitSize int) string func FormatFloat(f float64, fmt byte, prec, bitSize int) string func FormatInt(i int64, base int) string func FormatUint(i uint64, base int) string // 报告符文是否被Unicode定义为图形 func IsGraphic(r rune) bool // 是否是可打印的 func IsPrint(r rune) bool // 如"true"转为 true func ParseBool(str string) (bool, error) func ParseComplex(s string, bitSize int) (complex128, error) func ParseFloat(s string, bitSize int) (float64, error) func ParseInt(s string, base int, bitSize int) (i int64, err error) func ParseUint(s string, base int, bitSize int) (uint64, error) // Quote 把传入的内容变为字符串 func Quote(s string) string func QuoteRune(r rune) string func QuoteRuneToASCII(r rune) string func QuoteRuneToGraphic(r rune) string func QuoteToASCII(s string) string func QuoteToGraphic(s string) string func Unquote(s string) (string, error) func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error) ``` ## 示例 ### AppendBool ``` b := []byte("a1") b = strconv.AppendBool(b, false) b = strconv.AppendBool(b, true) fmt.Println(string(b)) // abc123falsetrue ``` ### AppendInt ``` b10 := []byte("int (base 10):") b10 = strconv.AppendInt(b10, 42, 10) fmt.Println(string(b10)) // int (base 10):42 ``` ### FromBool ``` b := strconv.FormatBool(true) fmt.Printf("%+v\n", b) // true ``` ### QuoteRune ``` s := strconv.QuoteRune('☺') fmt.Printf("%+v\n", s == "'☺'") // true ``` ### QuoteToASCII 转ASCII ``` s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`) fmt.Println(s) // "\"Fran & Freddie's Diner\t\u263a\"" ``` ### Unquote 去掉引号 ``` s, _ = strconv.Unquote("\"The string must be either double-quoted\"") fmt.Printf("%q\n", s) // "The string must be either double-quoted" s, err = strconv.Unquote("'\u263a'") fmt.Printf("%q, %v\n", s, err) // "☺", ```
';