转换 | strconv

打包strconv

  • import "strconv"

  • 概观

  • 索引

  • 示例

概观

包strconv实现了对基本数据类型的字符串表示的转换。

Numeric Conversions

最常见的数值转换是 Atoi(string to int)和 Itoa(int to string)。

i, err := strconv.Atoi("-42") s := strconv.Itoa(-42)

这些假设小数和 Go int 类型。

ParseBool,ParseFloat,ParseInt 和 ParseUint 将字符串转换为值:

b, err := strconv.ParseBool("true") f, err := strconv.ParseFloat("3.1415", 64) i, err := strconv.ParseInt("-42", 10, 64) u, err := strconv.ParseUint("42", 10, 64)

解析函数返回最宽的类型(float64,int64和uint64),但如果 size 参数指定较窄的宽度,则结果可以转换为较窄的类型而不会丢失数据:

s := "2147483647" // biggest int32 i64, err := strconv.ParseInt(s, 10, 32) ... i := int32(i64)

FormatBool,FormatFloat,FormatInt 和 FormatUint将值转换为字符串:

s := strconv.FormatBool(true) s := strconv.FormatFloat(3.1415, 'E', -1, 64) s := strconv.FormatInt(-42, 16) s := strconv.FormatUint(42, 16)

AppendBool,AppendFloat,AppendInt 和 AppendUint 是相似的,但将格式化后的值附加到目标切片。

字符串转换

Quote 和 QuoteToASCII 将字符串转换为带引号的字符串文字。后者保证结果是一个 ASCII 字符串,用 \ u 转义任何非 ASCII 的 Unicode:

q := Quote("Hello, 世界") q := QuoteToASCII("Hello, 世界")

QuoteRune 和 QuoteRuneToASCII 类似,但接受符文并返回引用的去符文字面值。

Unquote 和 UnquoteChar 不引用 Go 字符串和符文字面值。

索引

  • 常量

  • 变量

  • 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 CanBackquote(s string) bool

  • func FormatBool(b bool) 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

  • func IsGraphic(r rune) bool

  • func IsPrint(r rune) bool

  • func Itoa(i int) string

  • func ParseBool(str string) (bool, 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)

  • 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)

  • type NumError

  • func (e *NumError) Error() string

示例

AppendBool

打包文件

atob.go atof.go atoi.go decimal.go doc.go extfloat.go ftoa.go isprint.go itoa.go quote.go

常量

IntSize 是 int 或 uint 值的大小(以位为单位)。

const IntSize = intSize

变量

ErrRange 表示目标类型的值超出范围。

var ErrRange = errors.New("value out of range")

ErrSyntax 表明一个值没有针对目标类型的正确语法。

var ErrSyntax = errors.New("invalid syntax")

func AppendBoolSource

func AppendBool(dst []byte, b bool) []byte

AppendBool 根据 b 的值将“true”或“false”附加到 dst 并返回扩展缓冲区。

示例

package main import ( "fmt" "strconv" ) func main() { b := []byte("bool:") b = strconv.AppendBool(b, true) fmt.Println(string(b)) }

func AppendFloatSource

func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte

AppendFloat 将由 FormatFloat 生成的浮点数 f 的字符串形式附加到 dst 并返回扩展缓冲区。

示例

package main import ( "fmt" "strconv" ) func main() { b32 := []byte("float32:") b32 = strconv.AppendFloat(b32, 3.1415926535, 'E', -1, 32) fmt.Println(string(b32)) b64 := []byte("float64:") b64 = strconv.AppendFloat(b64, 3.1415926535, 'E', -1, 64) fmt.Println(string(b64)) }

func AppendIntSource

func AppendInt(dst []byte, i int64, base int) []byte

AppendInt 将由 FormatInt 生成的整数i的字符串形式附加到 dst 并返回扩展缓冲区。

示例

package main import ( "fmt" "strconv" ) func main() { b10 := []byte("int (base 10):") b10 = strconv.AppendInt(b10, -42, 10) fmt.Println(string(b10)) b16 := []byte("int (base 16):") b16 = strconv.AppendInt(b16, -42, 16) fmt.Println(string(b16)) }

func AppendQuoteSource

func AppendQuote(dst []byte, s string) []byte

AppendQuote 将由 Quote 生成的代表 s 的双引号 Go 字符串文字附加到 dst 并返回扩展缓冲区。

示例

package main import ( "fmt" "strconv" ) func main() { b := []byte("quote:") b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`) fmt.Println(string(b)) }

func AppendQuoteRuneSource

func AppendQuoteRune(dst []byte, r rune) []byte

AppendQuoteRune 将由 QuoteRune 生成的表示符文的单引号 Go 字符文字附加到 dst 并返回扩展缓冲区。

示例

package main import ( "fmt" "strconv" ) func main() { b := []byte("rune:") b = strconv.AppendQuoteRune(b, '☺') fmt.Println(string(b)) }

func AppendQuoteRuneToASCIISource

func AppendQuoteRuneToASCII(dst []byte, r rune) []byte

AppendQuoteRuneToASCII 将由 QuoteRuneToASCII 生成的代表该符文的单引号 Go 字符文字附加到 dst 并返回扩展缓冲区。

示例

package main import ( "fmt" "strconv" ) func main() { b := []byte("rune (ascii):") b = strconv.AppendQuoteRuneToASCII(b, '☺') fmt.Println(string(b)) }

func AppendQuoteRuneToGraphicSource

func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte

AppendQuoteRuneToGraphic 将由 QuoteRuneToGraphic 生成的表示符文的单引号 Go 字符文字附加到 dst 并返回扩展缓冲区。

func AppendQuoteToASCIISource

func AppendQuoteToASCII(dst []byte, s string) []byte

AppendQuoteToASCII 将由 QuoteToASCII 生成的代表 s 的双引号 Go 字符串文字附加到 dst 并返回扩展缓冲区。

示例

package main import ( "fmt" "strconv" ) func main() { b := []byte("quote (ascii):") b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`) fmt.Println(string(b)) }

func AppendQuoteToGraphicSource

func AppendQuoteToGraphic(dst []byte, s string) []byte

AppendQuoteToGraphic 将由 QuoteToGraphic 生成的代表 s 的双引号 Go 字符串文字附加到 dst 并返回扩展缓冲区。

func AppendUintSource

func AppendUint(dst []byte, i uint64, base int) []byte

AppendUint 将由 FormatUint 生成的无符号整数 i 的字符串形式附加到 dst 并返回扩展缓冲区。

示例

package main import ( "fmt" "strconv" ) func main() { b10 := []byte("uint (base 10):") b10 = strconv.AppendUint(b10, 42, 10) fmt.Println(string(b10)) b16 := []byte("uint (base 16):") b16 = strconv.AppendUint(b16, 42, 16) fmt.Println(string(b16)) }

func AtoiSource

func Atoi(s string) (int, error)

Atoi 返回 ParseInt(s, 10, 0) 转换为 int 类型的结果。

示例

package main import ( "fmt" "strconv" ) func main() { v := "10" if s, err := strconv.Atoi(v err == nil { fmt.Printf("%T, %v", s, s) } }

func CanBackquoteSource

func CanBackquote(s string) bool

CanBackquote 报告字符串 s 是否可以不改变为单行反引号字符串,而不包含 tab 以外的控制字符。

示例

package main import ( "fmt" "strconv" ) func main() { fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺")) fmt.Println(strconv.CanBackquote("`can't backquote this`")) }

func FormatBoolSource

func FormatBool(b bool) string

FormatBool 根据 b 的值返回“true”或“false”

示例

package main import ( "fmt" "strconv" ) func main() { v := true s := strconv.FormatBool(v) fmt.Printf("%T, %v\n", s, s) }

func FormatFloatSource

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

FormatFloat 根据格式 fmt 和 precision prec 将浮点数f转换为字符串。它将结果进行四舍五入,假设原始数据是从 bitSize 位的浮点值获得的(float32为32,float64为64)。

格式 fmt 是 'b'(-ddddp±ddd,二进制指数),'e'(-d.dddde±dd,十进制指数),'E'(-d.ddddE±dd,十进制指数),'f'(-ddd.dddd,无指数),'g'('e'表示大指数,'f'表示否则)或 'G'('E'表示大指数,否则'f')。

precision prec 控制由 'e','E','f','g' 和 'G' 格式打印的位数(不包括指数)。对于 'e','E' 和 'f',它是小数点后的位数。对于 'g' 和 'G' 这是总位数。特殊精度-1使用必需的最小位数,以便 ParseFloat 完全返回 f 。

示例

package main import ( "fmt" "strconv" ) func main() { v := 3.1415926535 s32 := strconv.FormatFloat(v, 'E', -1, 32) fmt.Printf("%T, %v\n", s32, s32) s64 := strconv.FormatFloat(v, 'E', -1, 64) fmt.Printf("%T, %v\n", s64, s64) }

func FormatIntSource

func FormatInt(i int64, base int) string

FormatInt 返回给定基数中的i的字符串表示,对于2 <= base <= 36.结果对于数字值> = 10使用小写字母 'a' 到 'z' 。

示例

package main import ( "fmt" "strconv" ) func main() { v := int64(-42) s10 := strconv.FormatInt(v, 10) fmt.Printf("%T, %v\n", s10, s10) s16 := strconv.FormatInt(v, 16) fmt.Printf("%T, %v\n", s16, s16) }

func FormatUintSource

func FormatUint(i uint64, base int) string

FormatUint 返回给定基数中的 i 的字符串表示,对于2 <= base <= 36.结果对于数字值> = 10使用小写字母 'a' 到 'z' 。

示例

package main import ( "fmt" "strconv" ) func main() { v := uint64(42) s10 := strconv.FormatUint(v, 10) fmt.Printf("%T, %v\n", s10, s10) s16 := strconv.FormatUint(v, 16) fmt.Printf("%T, %v\n", s16, s16) }

func IsGraphicSource

func IsGraphic(r rune) bool

IsGraphic 报告符文是否被 Unicode 定义为 Graphic。这些字符包括类别 L,M,N,P,S 和 Z 中的字母,标记,数字,标点,符号和空格。

func IsPrintSource

func IsPrint(r rune) bool

IsPrint 报告该符文是否被 Go 定义为可打印,其定义与 unicode.IsPrint 相同:字母,数字,标点,符号和 ASCII 空格。

示例

package main import ( "fmt" "strconv" ) func main() { c := strconv.IsPrint('\u263a') fmt.Println(c) bel := strconv.IsPrint('\007') fmt.Println(bel) }

func ItoaSource

func Itoa(i int) string

Itoa 是 FormatInt(int64(i), 10) 的缩写。

示例

package main import ( "fmt" "strconv" ) func main() { i := 10 s := strconv.Itoa(i) fmt.Printf("%T, %v\n", s, s) }

func ParseBoolSource

func ParseBool(str string) (bool, error)

ParseBool 返回字符串表示的布尔值。它接受1,t,T,TRUE,true,True,0,f,F,FALSE,false,False。任何其他值都会返回错误。

示例

package main import ( "fmt" "strconv" ) func main() { v := "true" if s, err := strconv.ParseBool(v err == nil { fmt.Printf("%T, %v\n", s, s) } }

func ParseFloatSource

func ParseFloat(s string, bitSize int) (float64, error)

ParseFloat 将字符串 s 转换为浮点数,精度由 bitSize:32指定,float32为64; float64为64。当 bitSize = 32时,结果仍然具有 float64 类型,但可以在不更改其值的情况下将其转换为 float32。

如果s格式良好且接近有效的浮点数,则 ParseFloat 返回使用 IEEE754 无偏舍入舍入的最近浮点数。

ParseFloat 返回的错误具有具体类型 * NumError 并包含 err.Num = s。

如果 s 在语法上不是格式良好的,ParseFloat 返回 err.Err = ErrSyntax。

如果 s 在语法上格式良好,但距离给定大小的最大浮点数大于1/2 ULP,则 ParseFloat 返回 f =±Inf,err.Err = ErrRange。

示例

package main import ( "fmt" "strconv" ) func main() { v := "3.1415926535" if s, err := strconv.ParseFloat(v, 32 err == nil { fmt.Printf("%T, %v\n", s, s) } if s, err := strconv.ParseFloat(v, 64 err == nil { fmt.Printf("%T, %v\n", s, s) } }

func ParseIntSource

func ParseInt(s string, base int, bitSize int) (i int64, err error)

ParseInt 解释给定基础(2到36)中的字符串 s 并返回相应的值 i。如果 base == 0,则基数由字符串的前缀隐含:base 16代表“0x”,base 8代表“0”,否则以10为底数。

bitSize 参数指定结果必须适合的整数类型。位大小 0,8,16,32 和 64 对应于 int,int8,int16,int32 和 int64。

ParseInt 返回的错误具有具体类型 * NumError 并包含err.Num = s。如果s为空或包含无效数字,则 err.Err = ErrSyntax,返回值为0; 如果与s对应的值不能用给定大小的有符号整数表示,则 err.Err = ErrRange,返回的值是相应 bitSize 和符号的最大幅度整数。

示例

package main import ( "fmt" "strconv" ) func main() { 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) } 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) } }

func ParseUintSource

func ParseUint(s string, base int, bitSize int) (uint64, error)

ParseUint 就像 ParseInt,但是对于无符号数字。

示例

package main import ( "fmt" "strconv" ) func main() { v := "42" if s, err := strconv.ParseUint(v, 10, 32 err == nil { fmt.Printf("%T, %v\n", s, s) } if s, err := strconv.ParseUint(v, 10, 64 err == nil { fmt.Printf("%T, %v\n", s, s) } }

func QuoteSource

func Quote(s string) string

Quote 返回一个双引号的 Go 字符串字面表示s。返回的字符串使用 Go 转义序列 (\t, \n, \xFF, \u0100) 作为 IsPrint 定义的控制字符和非可打印字符。

示例

package main import ( "fmt" "strconv" ) func main() { s := strconv.Quote(`"Fran & Freddie's Diner ☺"`) fmt.Println(s) }

func QuoteRuneSource

func QuoteRune(r rune) string

QuoteRune 返回一个表示符文的单引号 Go 字符。返回的字符串使用 Go 转义序列(\t, \n, \xFF, \u0100) 作为 IsPrint 定义的控制字符和非可打印字符。

示例

package main import ( "fmt" "strconv" ) func main() { s := strconv.QuoteRune('☺') fmt.Println(s) }

func QuoteRuneToASCIISource

func QuoteRuneToASCII(r rune) string

QuoteRuneToASCII 返回表示符文的单引号 Go 字符。对于非 ASCII 字符和 IsPrint 定义的非可打印字符,返回的字符串使用 Go 转义序列 (\t, \n, \xFF, \u0100)。

示例

package main import ( "fmt" "strconv" ) func main() { s := strconv.QuoteRuneToASCII('☺') fmt.Println(s) }

func QuoteRuneToGraphicSource

func QuoteRuneToGraphic(r rune) string

QuoteRuneToGraphic 返回代表符文的单引号 Go 字符。对于非 ASCII 字符和 IsGraphic 定义的非可打印字符,返回的字符串使用Go转义序列 (\t, \n, \xFF, \u0100)。

func QuoteToASCIISource

func QuoteToASCII(s string) string

QuoteToASCII 返回一个代表 s 的双引号 Go 字符串。对于非 ASCII 字符和 IsPrint 定义的非可打印字符,返回的字符串使用 Go 转义序列 (\t, \n, \xFF, \u0100) 。

示例

package main import ( "fmt" "strconv" ) func main() { s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`) fmt.Println(s) }

func QuoteToGraphicSource

func QuoteToGraphic(s string) string

QuoteToGraphic 返回一个代表 s 的双引号 Go 字符串。对于非 ASCII 字符和 IsGraphic 定义的非可打印字符,返回的字符串使用 Go 转义序列 (\t, \n, \xFF, \u0100)。

func UnquoteSource

func Unquote(s string) (string, error)

Unquote 将 s 解释为单引号,双引号或反引号的 Go 字符串文字,返回引用的字符串值。(如果 s 是单引号,它将是一个 Go 字符字面量; Unquote 会返回相应的一个字符字符串。)

示例

package main import ( "fmt" "strconv" ) func main() { test := func(s string) { t, err := strconv.Unquote(s) if err != nil { fmt.Printf("Unquote(%#v): %v\n", s, err) } else { fmt.Printf("Unquote(%#v) = %v\n", s, t) } } s := `\"Fran & Freddie's Diner\t\u263a\"\"` // If the string doesn't have quotes, it can't be unquoted. test(s) // invalid syntax test("`" + s + "`") test(`"` + s + `"`) test(`'\u263a'`) }

func UnquoteCharSource

func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)

UnquoteChar 解码转义字符串中的第一个字符或字节或由字符串 s 表示的字符字面值。它返回四个值:

1) value, the decoded Unicode code point or byte value; 2) multibyte, a boolean indicating whether the decoded character requires a multibyte UTF-8 representation; 3) tail, the remainder of the string after the character; and 4) an error that will be nil if the character is syntactically valid.

第二个参数 quote 指定了被解析的文字类型,因此允许使用哪个转义引号字符。如果设置为单引号,则允许序列 \'并且不允许未转义'。如果设置为双引号,则允许 \“并禁止未转义”。如果设置为零,它不允许任何转义,并允许两个引号字符显示为未转义。

示例

package main import ( "fmt" "log" "strconv" ) func main() { v, mb, t, err := strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"') if err != nil { log.Fatal(err) } fmt.Println("value:", string(v)) fmt.Println("multibyte:", mb) fmt.Println("tail:", t) }

type NumErrorSource

NumError 记录转换失败。

type NumError struct { Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat) Num string // the input Err error // the reason the conversion failed (ErrRange, ErrSyntax) }

示例

package main import ( "fmt" "strconv" ) func main() { str := "Not a number" if _, err := strconv.ParseFloat(str, 64 err != nil { e := err.(*strconv.NumError) fmt.Println("Func:", e.Func) fmt.Println("Num:", e.Num) fmt.Println("Err:", e.Err) fmt.Println(err) } }

func (*NumError) ErrorSource

func (e *NumError) Error() string