?
This document uses PHP Chinese website manual Release
import "strconv"
概觀
索引
示例
包strconv實(shí)現(xiàn)了對(duì)基本數(shù)據(jù)類型的字符串表示的轉(zhuǎn)換。
最常見的數(shù)值轉(zhuǎn)換是 Atoi(string to int)和 Itoa(int to string)。
i, err := strconv.Atoi("-42")s := strconv.Itoa(-42)
這些假設(shè)小數(shù)和 Go int 類型。
ParseBool,ParseFloat,ParseInt 和 ParseUint 將字符串轉(zhuǎn)換為值:
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)
解析函數(shù)返回最寬的類型(float64,int64和uint64),但如果 size 參數(shù)指定較窄的寬度,則結(jié)果可以轉(zhuǎn)換為較窄的類型而不會(huì)丟失數(shù)據(jù):
s := "2147483647" // biggest int32i64, err := strconv.ParseInt(s, 10, 32)...i := int32(i64)
FormatBool,F(xiàn)ormatFloat,F(xiàn)ormatInt 和 FormatUint將值轉(zhuǎn)換為字符串:
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 是相似的,但將格式化后的值附加到目標(biāo)切片。
Quote 和 QuoteToASCII 將字符串轉(zhuǎn)換為帶引號(hào)的字符串文字。后者保證結(jié)果是一個(gè) ASCII 字符串,用 \ u 轉(zhuǎn)義任何非 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 AppendFloat AppendInt AppendQuote AppendQuoteRune AppendQuoteRuneToASCII AppendQuoteToASCII AppendUint Atoi CanBackquote FormatBool FormatFloat FormatInt FormatUint IsPrint Itoa NumError ParseBool ParseFloat ParseInt ParseUint Quote QuoteRune QuoteRuneToASCII QuoteToASCII Unquote UnquoteChar
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 表示目標(biāo)類型的值超出范圍。
var ErrRange = errors.New("value out of range")
ErrSyntax 表明一個(gè)值沒有針對(duì)目標(biāo)類型的正確語(yǔ)法。
var ErrSyntax = errors.New("invalid syntax")
func AppendBool(dst []byte, b bool) []byte
AppendBool 根據(jù) b 的值將“true”或“false”附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
package mainimport ("fmt""strconv")func main() { b := []byte("bool:") b = strconv.AppendBool(b, true) fmt.Println(string(b))}
func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte
AppendFloat 將由 FormatFloat 生成的浮點(diǎn)數(shù) f 的字符串形式附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
package mainimport ("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 AppendInt(dst []byte, i int64, base int) []byte
AppendInt 將由 FormatInt 生成的整數(shù)i的字符串形式附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
package mainimport ("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 AppendQuote(dst []byte, s string) []byte
AppendQuote 將由 Quote 生成的代表 s 的雙引號(hào) Go 字符串文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
package mainimport ("fmt""strconv")func main() { b := []byte("quote:") b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`) fmt.Println(string(b))}
func AppendQuoteRune(dst []byte, r rune) []byte
AppendQuoteRune 將由 QuoteRune 生成的表示符文的單引號(hào) Go 字符文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
package mainimport ("fmt""strconv")func main() { b := []byte("rune:") b = strconv.AppendQuoteRune(b, '?') fmt.Println(string(b))}
func AppendQuoteRuneToASCII(dst []byte, r rune) []byte
AppendQuoteRuneToASCII 將由 QuoteRuneToASCII 生成的代表該符文的單引號(hào) Go 字符文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
package mainimport ("fmt""strconv")func main() { b := []byte("rune (ascii):") b = strconv.AppendQuoteRuneToASCII(b, '?') fmt.Println(string(b))}
func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte
AppendQuoteRuneToGraphic 將由 QuoteRuneToGraphic 生成的表示符文的單引號(hào) Go 字符文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
func AppendQuoteToASCII(dst []byte, s string) []byte
AppendQuoteToASCII 將由 QuoteToASCII 生成的代表 s 的雙引號(hào) Go 字符串文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
package mainimport ("fmt""strconv")func main() { b := []byte("quote (ascii):") b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`) fmt.Println(string(b))}
func AppendQuoteToGraphic(dst []byte, s string) []byte
AppendQuoteToGraphic 將由 QuoteToGraphic 生成的代表 s 的雙引號(hào) Go 字符串文字附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
func AppendUint(dst []byte, i uint64, base int) []byte
AppendUint 將由 FormatUint 生成的無(wú)符號(hào)整數(shù) i 的字符串形式附加到 dst 并返回?cái)U(kuò)展緩沖區(qū)。
package mainimport ("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 Atoi(s string) (int, error)
Atoi 返回 ParseInt(s, 10, 0) 轉(zhuǎn)換為 int 類型的結(jié)果。
package mainimport ("fmt""strconv")func main() { v := "10"if s, err := strconv.Atoi(v); err == nil { fmt.Printf("%T, %v", s, s)}}
func CanBackquote(s string) bool
CanBackquote 報(bào)告字符串 s 是否可以不改變?yōu)閱涡蟹匆?hào)字符串,而不包含 tab 以外的控制字符。
package mainimport ("fmt""strconv")func main() { fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ?")) fmt.Println(strconv.CanBackquote("`can't backquote this`"))}
func FormatBool(b bool) string
FormatBool 根據(jù) b 的值返回“true”或“false”
package mainimport ("fmt""strconv")func main() { v := true s := strconv.FormatBool(v) fmt.Printf("%T, %v\n", s, s)}
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
FormatFloat 根據(jù)格式 fmt 和 precision prec 將浮點(diǎn)數(shù)f轉(zhuǎn)換為字符串。它將結(jié)果進(jìn)行四舍五入,假設(shè)原始數(shù)據(jù)是從 bitSize 位的浮點(diǎn)值獲得的(float32為32,float64為64)。
格式 fmt 是 'b'(-ddddp±ddd,二進(jìn)制指數(shù)),'e'(-d.dddde±dd,十進(jìn)制指數(shù)),'E'(-d.ddddE±dd,十進(jìn)制指數(shù)),'f'(-ddd.dddd,無(wú)指數(shù)),'g'('e'表示大指數(shù),'f'表示否則)或 'G'('E'表示大指數(shù),否則'f')。
precision prec 控制由 'e','E','f','g' 和 'G' 格式打印的位數(shù)(不包括指數(shù))。對(duì)于 'e','E' 和 'f',它是小數(shù)點(diǎn)后的位數(shù)。對(duì)于 'g' 和 'G' 這是總位數(shù)。特殊精度-1使用必需的最小位數(shù),以便 ParseFloat 完全返回 f 。
package mainimport ("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 FormatInt(i int64, base int) string
FormatInt 返回給定基數(shù)中的i的字符串表示,對(duì)于2 <= base <= 36.結(jié)果對(duì)于數(shù)字值> = 10使用小寫字母 'a' 到 'z' 。
package mainimport ("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 FormatUint(i uint64, base int) string
FormatUint 返回給定基數(shù)中的 i 的字符串表示,對(duì)于2 <= base <= 36.結(jié)果對(duì)于數(shù)字值> = 10使用小寫字母 'a' 到 'z' 。
package mainimport ("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 IsGraphic(r rune) bool
IsGraphic 報(bào)告符文是否被 Unicode 定義為 Graphic。這些字符包括類別 L,M,N,P,S 和 Z 中的字母,標(biāo)記,數(shù)字,標(biāo)點(diǎn),符號(hào)和空格。
func IsPrint(r rune) bool
IsPrint 報(bào)告該符文是否被 Go 定義為可打印,其定義與 unicode.IsPrint 相同:字母,數(shù)字,標(biāo)點(diǎn),符號(hào)和 ASCII 空格。
package mainimport ("fmt""strconv")func main() { c := strconv.IsPrint('\u263a') fmt.Println(c) bel := strconv.IsPrint('\007') fmt.Println(bel)}
func Itoa(i int) string
Itoa 是 FormatInt(int64(i), 10) 的縮寫。
package mainimport ("fmt""strconv")func main() { i := 10 s := strconv.Itoa(i) fmt.Printf("%T, %v\n", s, s)}
func ParseBool(str string) (bool, error)
ParseBool 返回字符串表示的布爾值。它接受1,t,T,TRUE,true,True,0,f,F(xiàn),F(xiàn)ALSE,false,F(xiàn)alse。任何其他值都會(huì)返回錯(cuò)誤。
package mainimport ("fmt""strconv")func main() { v := "true"if s, err := strconv.ParseBool(v); err == nil { fmt.Printf("%T, %v\n", s, s)}}
func ParseFloat(s string, bitSize int) (float64, error)
ParseFloat 將字符串 s 轉(zhuǎn)換為浮點(diǎn)數(shù),精度由 bitSize:32指定,float32為64; float64為64。當(dāng) bitSize = 32時(shí),結(jié)果仍然具有 float64 類型,但可以在不更改其值的情況下將其轉(zhuǎn)換為 float32。
如果s格式良好且接近有效的浮點(diǎn)數(shù),則 ParseFloat 返回使用 IEEE754 無(wú)偏舍入舍入的最近浮點(diǎn)數(shù)。
ParseFloat 返回的錯(cuò)誤具有具體類型 * NumError 并包含 err.Num = s。
如果 s 在語(yǔ)法上不是格式良好的,ParseFloat 返回 err.Err = ErrSyntax。
如果 s 在語(yǔ)法上格式良好,但距離給定大小的最大浮點(diǎn)數(shù)大于1/2 ULP,則 ParseFloat 返回 f =±Inf,err.Err = ErrRange。
package mainimport ("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 ParseInt(s string, base int, bitSize int) (i int64, err error)
ParseInt 解釋給定基礎(chǔ)(2到36)中的字符串 s 并返回相應(yīng)的值 i。如果 base == 0,則基數(shù)由字符串的前綴隱含:base 16代表“0x”,base 8代表“0”,否則以10為底數(shù)。
bitSize 參數(shù)指定結(jié)果必須適合的整數(shù)類型。位大小 0,8,16,32 和 64 對(duì)應(yīng)于 int,int8,int16,int32 和 int64。
ParseInt 返回的錯(cuò)誤具有具體類型 * NumError 并包含err.Num = s。如果s為空或包含無(wú)效數(shù)字,則 err.Err = ErrSyntax,返回值為0; 如果與s對(duì)應(yīng)的值不能用給定大小的有符號(hào)整數(shù)表示,則 err.Err = ErrRange,返回的值是相應(yīng) bitSize 和符號(hào)的最大幅度整數(shù)。
package mainimport ("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 ParseUint(s string, base int, bitSize int) (uint64, error)
ParseUint 就像 ParseInt,但是對(duì)于無(wú)符號(hào)數(shù)字。
package mainimport ("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 Quote(s string) string
Quote 返回一個(gè)雙引號(hào)的 Go 字符串字面表示s。返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100) 作為 IsPrint 定義的控制字符和非可打印字符。
package mainimport ("fmt""strconv")func main() { s := strconv.Quote(`"Fran & Freddie's Diner ?"`) fmt.Println(s)}
func QuoteRune(r rune) string
QuoteRune 返回一個(gè)表示符文的單引號(hào) Go 字符。返回的字符串使用 Go 轉(zhuǎn)義序列(\t, \n, \xFF, \u0100) 作為 IsPrint 定義的控制字符和非可打印字符。
package mainimport ("fmt""strconv")func main() { s := strconv.QuoteRune('?') fmt.Println(s)}
func QuoteRuneToASCII(r rune) string
QuoteRuneToASCII 返回表示符文的單引號(hào) Go 字符。對(duì)于非 ASCII 字符和 IsPrint 定義的非可打印字符,返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100)。
package mainimport ("fmt""strconv")func main() { s := strconv.QuoteRuneToASCII('?') fmt.Println(s)}
func QuoteRuneToGraphic(r rune) string
QuoteRuneToGraphic 返回代表符文的單引號(hào) Go 字符。對(duì)于非 ASCII 字符和 IsGraphic 定義的非可打印字符,返回的字符串使用Go轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100)。
func QuoteToASCII(s string) string
QuoteToASCII 返回一個(gè)代表 s 的雙引號(hào) Go 字符串。對(duì)于非 ASCII 字符和 IsPrint 定義的非可打印字符,返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100) 。
package mainimport ("fmt""strconv")func main() { s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ?"`) fmt.Println(s)}
func QuoteToGraphic(s string) string
QuoteToGraphic 返回一個(gè)代表 s 的雙引號(hào) Go 字符串。對(duì)于非 ASCII 字符和 IsGraphic 定義的非可打印字符,返回的字符串使用 Go 轉(zhuǎn)義序列 (\t, \n, \xFF, \u0100)。
func Unquote(s string) (string, error)
Unquote 將 s 解釋為單引號(hào),雙引號(hào)或反引號(hào)的 Go 字符串文字,返回引用的字符串值。(如果 s 是單引號(hào),它將是一個(gè) Go 字符字面量; Unquote 會(huì)返回相應(yīng)的一個(gè)字符字符串。)
package mainimport ("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 syntaxtest("`" + s + "`")test(`"` + s + `"`)test(`'\u263a'`)}
func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
UnquoteChar 解碼轉(zhuǎn)義字符串中的第一個(gè)字符或字節(jié)或由字符串 s 表示的字符字面值。它返回四個(gè)值:
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; and4) an error that will be nil if the character is syntactically valid.
第二個(gè)參數(shù) quote 指定了被解析的文字類型,因此允許使用哪個(gè)轉(zhuǎn)義引號(hào)字符。如果設(shè)置為單引號(hào),則允許序列 \'并且不允許未轉(zhuǎn)義'。如果設(shè)置為雙引號(hào),則允許 \“并禁止未轉(zhuǎn)義”。如果設(shè)置為零,它不允許任何轉(zhuǎn)義,并允許兩個(gè)引號(hào)字符顯示為未轉(zhuǎn)義。
package mainimport ("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)}
NumError 記錄轉(zhuǎn)換失敗。
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 mainimport ("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 (e *NumError) Error() string