package strconv
import "strconv"
Package strconv 实现了基本数据类型与字符串表示之间的相互转换。
数值转换
最常见的数值转换是 Atoi(字符串转 int)和 Itoa(int 转字符串)。
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 将字符串转换为带引号的 Go 字符串字面量。后者通过使用 \u 转义任何非 ASCII Unicode 字符,确保结果是 ASCII 字符串:
q := strconv.Quote("Hello, 世界")
q := strconv.QuoteToASCII("Hello, 世界")
QuoteRune 和 QuoteRuneToASCII 类似,但接受 rune 并返回带引号的 Go rune 字面量。
Unquote 和 UnquoteChar 对 Go 字符串和 rune 字面量进行解引用。
Index
- Constants
- Variables
- 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 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
- func IsGraphic(r rune) bool
- func IsPrint(r rune) bool
- func Itoa(i int) string
- 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)
- 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 QuotedPrefix(s string) (string, error)
- func Unquote(s string) (string, error)
- func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
- type NumError
Examples
- AppendBool
- AppendFloat
- AppendInt
- AppendQuote
- AppendQuoteRune
- AppendQuoteRuneToASCII
- AppendQuoteToASCII
- AppendUint
- Atoi
- CanBackquote
- FormatBool
- FormatFloat
- FormatInt
- FormatUint
- IsGraphic
- IsPrint
- Itoa
- NumError
- ParseBool
- ParseFloat
- ParseInt
- ParseUint
- Quote
- QuoteRune
- QuoteRuneToASCII
- QuoteRuneToGraphic
- QuoteToASCII
- QuoteToGraphic
- QuotedPrefix
- Unquote
- UnquoteChar
Constants
const IntSize = strconv.IntSize
IntSize 是 int 或 uint 值的位大小。
Variables
var ErrRange = errors.New("value out of range")
ErrRange 表示值超出目标类型的范围。
var ErrSyntax = errors.New("invalid syntax")
ErrSyntax 表示值不具有目标类型的正确语法。
Functions
func AppendBool
func AppendBool(dst []byte, b bool) []byte
AppendBool 根据 b 的值将 "true" 或 "false" 追加到 dst 并返回扩展后的缓冲区。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
b := []byte("bool:")
b = strconv.AppendBool(b, true)
fmt.Println(string(b))
}
bool:true
func AppendFloat
func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte
AppendFloat 将 FormatFloat 生成的浮点数 f 的字符串形式追加到 dst 并返回扩展后的缓冲区。
Output:Example
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))
}
float32:3.1415927E+00
float64:3.1415926535E+00
func AppendInt
func AppendInt(dst []byte, i int64, base int) []byte
AppendInt 将 FormatInt 生成的整数 i 的字符串形式追加到 dst 并返回扩展后的缓冲区。
Output:Example
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))
}
int (base 10):-42
int (base 16):-2a
func AppendQuote
func AppendQuote(dst []byte, s string) []byte
AppendQuote 将 Quote 生成的表示 s 的双引号 Go 字符串字面量追加到 dst 并返回扩展后的缓冲区。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
b := []byte("quote:")
b = strconv.AppendQuote(b, `"Fran & Freddie's Diner"`)
fmt.Println(string(b))
}
quote:"\"Fran & Freddie's Diner\""
func AppendQuoteRune
func AppendQuoteRune(dst []byte, r rune) []byte
AppendQuoteRune 将 QuoteRune 生成的表示该 rune 的单引号 Go 字符字面量追加到 dst 并返回扩展后的缓冲区。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
b := []byte("rune:")
b = strconv.AppendQuoteRune(b, '☺')
fmt.Println(string(b))
}
rune:'☺'
func AppendQuoteRuneToASCII
func AppendQuoteRuneToASCII(dst []byte, r rune) []byte
AppendQuoteRuneToASCII 将 QuoteRuneToASCII 生成的表示该 rune 的单引号 Go 字符字面量追加到 dst 并返回扩展后的缓冲区。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
b := []byte("rune (ascii):")
b = strconv.AppendQuoteRuneToASCII(b, '☺')
fmt.Println(string(b))
}
rune (ascii):'\u263a'
func AppendQuoteRuneToGraphic
func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte
AppendQuoteRuneToGraphic 将 QuoteRuneToGraphic 生成的表示该 rune 的单引号 Go 字符字面量追加到 dst 并返回扩展后的缓冲区。
func AppendQuoteToASCII
func AppendQuoteToASCII(dst []byte, s string) []byte
AppendQuoteToASCII 将 QuoteToASCII 生成的表示 s 的双引号 Go 字符串字面量追加到 dst 并返回扩展后的缓冲区。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
b := []byte("quote (ascii):")
b = strconv.AppendQuoteToASCII(b, `"Fran & Freddie's Diner"`)
fmt.Println(string(b))
}
quote (ascii):"\"Fran & Freddie's Diner\""
func AppendQuoteToGraphic
func AppendQuoteToGraphic(dst []byte, s string) []byte
AppendQuoteToGraphic 将 QuoteToGraphic 生成的表示 s 的双引号 Go 字符串字面量追加到 dst 并返回扩展后的缓冲区。
func AppendUint
func AppendUint(dst []byte, i uint64, base int) []byte
AppendUint 将 FormatUint 生成的无符号整数 i 的字符串形式追加到 dst 并返回扩展后的缓冲区。
Output:Example
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))
}
uint (base 10):42
uint (base 16):2a
func Atoi
func Atoi(s string) (int, error)
Atoi 等价于 ParseInt(s, 10, 0),并转换为 int 类型。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
v := "10"
if s, err := strconv.Atoi(v); err == nil {
fmt.Printf("%T, %v", s, s)
}
}
int, 10
func CanBackquote
func CanBackquote(s string) bool
CanBackquote 报告字符串 s 是否可以不变地表示为单行反引号字符串,其中除制表符外没有其他控制字符。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
fmt.Println(strconv.CanBackquote("Fran & Freddie's Diner ☺"))
fmt.Println(strconv.CanBackquote("`can't backquote this`"))
}
true
false
func FormatBool
func FormatBool(b bool) string
FormatBool 根据 b 的值返回 "true" 或 "false"。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
v := true
s := strconv.FormatBool(v)
fmt.Printf("%T, %v\n", s, s)
}
string, true
func FormatComplex
func FormatComplex(c complex128, fmt byte, prec, bitSize int) string
FormatComplex 将复数 c 转换为 (a+bi) 形式的字符串, 其中 a 和 b 为实部和虚部,根据格式 fmt 和精度 prec 格式化。
格式 fmt 和精度 prec 的含义与 FormatFloat 中相同。 它假设原始值是从 bitSize 位的复数值获得的,并对结果进行舍入, bitSize 对于 complex64 必须为 64,对于 complex128 必须为 128。
func FormatFloat
func FormatFloat(f float64, fmt byte, prec, bitSize int) string
FormatFloat 根据格式 fmt 和精度 prec 将浮点数 f 转换为字符串。 它假设原始值是从 bitSize 位的浮点值(32 对应 float32,64 对应 float64)获得的,并对结果进行舍入。
格式 fmt 为以下之一:
- 'b'(-ddddp±ddd,二进制指数),
- 'e'(-d.dddde±dd,十进制指数),
- 'E'(-d.ddddE±dd,十进制指数),
- 'f'(-ddd.dddd,无指数),
- 'g'(大指数用 'e',否则用 'f'),
- 'G'(大指数用 'E',否则用 'f'),
- 'x'(-0xd.ddddp±ddd,十六进制小数和二进制指数),或
- 'X'(-0Xd.ddddP±ddd,十六进制小数和二进制指数)。
精度 prec 控制 'e'、'E'、'f'、'g'、'G'、'x' 和 'X' 格式打印的位数(不包括指数)。
对于 'e'、'E'、'f'、'x' 和 'X',它是小数点后的位数。
对于 'g' 和 'G',它是有效数字的最大位数(尾随零被移除)。
特殊精度 -1 使用必要的最少位数,以使 ParseFloat 能精确返回 f。
指数写为十进制整数;
对于 'b' 以外的所有格式,它至少为两位数字。
Output:Example
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)
// fmt.Println uses these arguments to print floats
fmt64 := strconv.FormatFloat(v, 'g', -1, 64)
fmt.Printf("%T, %v\n", fmt64, fmt64)
}
string, 3.1415927E+00
string, 3.1415926535E+00
string, 3.1415926535
func FormatInt
func FormatInt(i int64, base int) string
FormatInt 返回 i 在给定进制下的字符串表示,进制范围为 2 <= base <= 36。
结果使用小写字母 'a' 到 'z' 表示 >= 10 的数字值。
Output:Example
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)
}
string, -42
string, -2a
func FormatUint
func FormatUint(i uint64, base int) string
FormatUint 返回 i 在给定进制下的字符串表示,进制范围为 2 <= base <= 36。
结果使用小写字母 'a' 到 'z' 表示 >= 10 的数字值。
Output:Example
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)
}
string, 42
string, 2a
func IsGraphic
func IsGraphic(r rune) bool
IsGraphic 报告该 rune 是否被 Unicode 定义为图形字符。此类字符包括字母、标记、数字、标点、符号和空格,来自类别 L、M、N、P、S 和 Zs。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
shamrock := strconv.IsGraphic('☘')
fmt.Println(shamrock)
a := strconv.IsGraphic('a')
fmt.Println(a)
bel := strconv.IsGraphic('\007')
fmt.Println(bel)
}
true
true
false
func IsPrint
func IsPrint(r rune) bool
IsPrint 报告该 rune 是否被 Go 定义为可打印,定义与 unicode.IsPrint 相同:字母、数字、标点、符号和 ASCII 空格。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
c := strconv.IsPrint('\u263a')
fmt.Println(c)
bel := strconv.IsPrint('\007')
fmt.Println(bel)
}
true
false
func Itoa
func Itoa(i int) string
Itoa 等价于 FormatInt(int64(i), 10)。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
i := 10
s := strconv.Itoa(i)
fmt.Printf("%T, %v\n", s, s)
}
string, 10
func ParseBool
func ParseBool(str string) (bool, error)
ParseBool 返回字符串表示的布尔值。
它接受 1、t、T、TRUE、true、True、0、f、F、FALSE、false、False。
任何其他值都会返回错误。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
v := "true"
if s, err := strconv.ParseBool(v); err == nil {
fmt.Printf("%T, %v\n", s, s)
}
}
bool, true
func ParseComplex
func ParseComplex(s string, bitSize int) (complex128, error)
ParseComplex 将字符串 s 转换为复数,精度由 bitSize 指定: 64 对应 complex64,128 对应 complex128。 当 bitSize=64 时,结果仍为 complex128 类型,但可在不改变其值的情况下转换为 complex64。
s 表示的数字必须为 N、Ni 或 N±Ni 形式,其中 N 代表 ParseFloat 可识别的浮点数, i 为虚部。若第二个 N 无符号,则两部分之间必须如 ± 所示使用 + 号。 若第二个 N 为 NaN,则仅接受 + 号。 该形式可加括号,且不能包含任何空格。 结果复数由 ParseFloat 转换后的两部分组成。
ParseComplex 返回的错误具体类型为 *NumError,且包含 err.Num = s。
若 s 语法格式不正确,ParseComplex 返回 err.Err = ErrSyntax。
若 s 语法格式正确,但任一部分超出对应大小最大浮点数的 1/2 ULP 范围, ParseComplex 返回 err.Err = ErrRange,且对应部分 c = ±Inf。
func ParseFloat
func ParseFloat(s string, bitSize int) (float64, error)
ParseFloat 将字符串 s 转换为浮点数,精度由 bitSize 指定: 32 对应 float32,64 对应 float64。 当 bitSize=32 时,结果仍为 float64 类型,但可在不改变其值的情况下转换为 float32。
ParseFloat 接受 Go 语法中 floating-point literals 定义的十进制和十六进制浮点数。 若 s 格式正确且接近有效浮点数,ParseFloat 使用 IEEE754 无偏舍入返回最接近的浮点数。 (仅当十六进制表示的位数超过尾数可容纳的位数时,解析十六进制浮点值才会进行舍入。)
ParseFloat 返回的错误具体类型为 *NumError,且包含 err.Num = s。
若 s 语法格式不正确,ParseFloat 返回 err.Err = ErrSyntax。
若 s 语法格式正确,但超出给定大小最大浮点数的 1/2 ULP 范围, ParseFloat 返回 f = ±Inf,err.Err = ErrRange。
ParseFloat 将字符串 "NaN" 以及(可能带符号的)字符串 "Inf" 和 "Infinity"
识别为对应的特殊浮点值。匹配时忽略大小写。
Output:Example
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)
}
if s, err := strconv.ParseFloat("NaN", 32); err == nil {
fmt.Printf("%T, %v\n", s, s)
}
// ParseFloat is case insensitive
if s, err := strconv.ParseFloat("nan", 32); err == nil {
fmt.Printf("%T, %v\n", s, s)
}
if s, err := strconv.ParseFloat("inf", 32); err == nil {
fmt.Printf("%T, %v\n", s, s)
}
if s, err := strconv.ParseFloat("+Inf", 32); err == nil {
fmt.Printf("%T, %v\n", s, s)
}
if s, err := strconv.ParseFloat("-Inf", 32); err == nil {
fmt.Printf("%T, %v\n", s, s)
}
if s, err := strconv.ParseFloat("-0", 32); err == nil {
fmt.Printf("%T, %v\n", s, s)
}
if s, err := strconv.ParseFloat("+0", 32); err == nil {
fmt.Printf("%T, %v\n", s, s)
}
}
float64, 3.1415927410125732
float64, 3.1415926535
float64, NaN
float64, NaN
float64, +Inf
float64, +Inf
float64, -Inf
float64, -0
float64, 0
func ParseInt
func ParseInt(s string, base int, bitSize int) (i int64, err error)
ParseInt 将字符串 s 按给定进制(0、2 到 36)和位大小(0 到 64)解释,并返回对应的值 i。
字符串可以以 "+" 或 "-" 开头。
若 base 参数为 0,则真正的进制由符号(若有)后的字符串前缀隐含: "0b" 对应 2,"0" 或 "0o" 对应 8,"0x" 对应 16,否则为 10。 此外,仅当 base 参数为 0 时,允许使用 Go 语法中 integer literals 定义的下划线字符。
bitSize 参数指定结果必须适配的整数类型。 位大小 0、8、16、32 和 64 分别对应 int、int8、int16、int32 和 int64。 若 bitSize 小于 0 或大于 64,则返回错误。
ParseInt 返回的错误具体类型为 *NumError,且包含 err.Num = s。
若 s 为空或包含无效数字,err.Err = ErrSyntax,返回值为 0;
若 s 对应的值无法用给定位大小的有符号整数表示,err.Err = ErrRange,
返回值为对应位大小和符号的最大幅度整数。
Output:Example
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)
}
}
int64, -354634382
int64, -3546343826724305832
func ParseUint
func ParseUint(s string, base int, bitSize int) (uint64, error)
ParseUint 与 ParseInt 类似,但用于无符号数。
不允许有符号前缀。
Output:Example
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)
}
}
uint64, 42
uint64, 42
func Quote
func Quote(s string) string
Quote 返回表示 s 的双引号 Go 字符串字面量。返回的字符串对控制字符和 IsPrint 定义的不可打印字符使用 Go 转义序列(\t、\n、\xFF、\u0100)。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
// This string literal contains a tab character.
s := strconv.Quote(`"Fran & Freddie's Diner ☺"`)
fmt.Println(s)
}
"\"Fran & Freddie's Diner\t☺\""
func QuoteRune
func QuoteRune(r rune) string
QuoteRune 返回表示该 rune 的单引号 Go 字符字面量。返回的字符串对控制字符和 IsPrint 定义的不可打印字符使用 Go 转义序列(\t、\n、\xFF、\u0100)。
如果 r 不是有效的 Unicode 码点,则将其解释为 Unicode 替换字符 U+FFFD。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
s := strconv.QuoteRune('☺')
fmt.Println(s)
}
'☺'
func QuoteRuneToASCII
func QuoteRuneToASCII(r rune) string
QuoteRuneToASCII 返回表示该 rune 的单引号 Go 字符字面量。返回的字符串对非 ASCII 字符和 IsPrint 定义的不可打印字符使用 Go 转义序列(\t、\n、\xFF、\u0100)。
如果 r 不是有效的 Unicode 码点,则将其解释为 Unicode 替换字符 U+FFFD。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
s := strconv.QuoteRuneToASCII('☺')
fmt.Println(s)
}
'\u263a'
func QuoteRuneToGraphic
func QuoteRuneToGraphic(r rune) string
QuoteRuneToGraphic 返回表示该 rune 的单引号 Go 字符字面量。如果该 rune 不是 IsGraphic 定义的 Unicode 图形字符,则返回的字符串将使用 Go 转义序列(\t、\n、\xFF、\u0100)。
如果 r 不是有效的 Unicode 码点,则将其解释为 Unicode 替换字符 U+FFFD。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
s := strconv.QuoteRuneToGraphic('☺')
fmt.Println(s)
s = strconv.QuoteRuneToGraphic('\u263a')
fmt.Println(s)
s = strconv.QuoteRuneToGraphic('\u000a')
fmt.Println(s)
s = strconv.QuoteRuneToGraphic(' ') // tab character
fmt.Println(s)
}
'☺'
'☺'
'\n'
'\t'
func QuoteToASCII
func QuoteToASCII(s string) string
QuoteToASCII 返回表示 s 的双引号 Go 字符串字面量。返回的字符串对非 ASCII 字符和 IsPrint 定义的不可打印字符使用 Go 转义序列(\t、\n、\xFF、\u0100)。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
// This string literal contains a tab character.
s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`)
fmt.Println(s)
}
"\"Fran & Freddie's Diner\t\u263a\""
func QuoteToGraphic
func QuoteToGraphic(s string) string
QuoteToGraphic 返回表示 s 的双引号 Go 字符串字面量。返回的字符串保持 IsGraphic 定义的 Unicode 图形字符不变,并对非图形字符使用 Go 转义序列(\t、\n、\xFF、\u0100)。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
s := strconv.QuoteToGraphic("☺")
fmt.Println(s)
// This string literal contains a tab character.
s = strconv.QuoteToGraphic("This is a \u263a \u000a")
fmt.Println(s)
s = strconv.QuoteToGraphic(`" This is a ☺ \n "`)
fmt.Println(s)
}
"☺"
"This is a ☺\t\n"
"\" This is a ☺ \\n \""
func QuotedPrefix
func QuotedPrefix(s string) (string, error)
QuotedPrefix 返回 s 前缀处的引用字符串(如 Unquote 所理解的)。
如果 s 不以有效的引用字符串开头,QuotedPrefix 返回错误。
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
s, err := strconv.QuotedPrefix("not a quoted string")
fmt.Printf("%q, %v\n", s, err)
s, err = strconv.QuotedPrefix("\"double-quoted string\" with trailing text")
fmt.Printf("%q, %v\n", s, err)
s, err = strconv.QuotedPrefix("`or backquoted` with more trailing text")
fmt.Printf("%q, %v\n", s, err)
s, err = strconv.QuotedPrefix("'\u263a' is also okay")
fmt.Printf("%q, %v\n", s, err)
}
"", invalid syntax
"\"double-quoted string\"", <nil>
"`or backquoted`", <nil>
"'☺'", <nil>
func Unquote
func Unquote(s string) (string, error)
Unquote 将 s 解释为单引号、双引号或反引号 Go 字符串字面量,返回 s 引用的字符串值。
(如果 s 是单引号,则它将是 Go 字符字面量;Unquote 返回相应的单字符字符串。对于空字符字面量,Unquote 返回空字符串。)
Output:Example
package main
import (
"fmt"
"strconv"
)
func main() {
s, err := strconv.Unquote("You can't unquote a string without quotes")
fmt.Printf("%q, %v\n", s, err)
s, err = strconv.Unquote("\"The string must be either double-quoted\"")
fmt.Printf("%q, %v\n", s, err)
s, err = strconv.Unquote("`or backquoted.`")
fmt.Printf("%q, %v\n", s, err)
s, err = strconv.Unquote("'\u263a'") // single character only allowed in single quotes
fmt.Printf("%q, %v\n", s, err)
s, err = strconv.Unquote("'\u2639\u2639'")
fmt.Printf("%q, %v\n", s, err)
}
"", invalid syntax
"The string must be either double-quoted", <nil>
"or backquoted.", <nil>
"☺", <nil>
"", invalid syntax
func UnquoteChar
func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
UnquoteChar 解码转义字符串或字符字面量表示的字符串 s 中的第一个字符或字节。 它返回四个值:
- value,解码后的 Unicode 码点或字节值;
- multibyte,一个布尔值,指示解码后的字符是否需要多字节 UTF-8 表示;
- tail,字符后字符串的剩余部分;
- 如果字符语法有效,则为 nil 的错误。
第二个参数 quote 指定正在解析的字面量类型,因此允许哪些转义引号字符。
如果设置为单引号,则允许序列 \' 并禁止未转义的 '。
如果设置为双引号,则允许 \" 并禁止未转义的 "。
如果设置为零,则不允许任何一种转义,并允许两个引号字符未转义出现。
Output:Example
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)
}
value: "
multibyte: false
tail: Fran & Freddie's Diner\"
Types
type NumError
type NumError struct { Func string // 失败的函数(ParseBool、ParseInt、ParseUint、ParseFloat、ParseComplex) Num string // 输入 Err error // 转换失败的原因(例如 ErrRange、ErrSyntax 等) }
NumError 记录失败的转换。
Output:Example
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: ParseFloat
Num: Not a number
Err: invalid syntax
strconv.ParseFloat: parsing "Not a number": invalid syntax
func (*NumError) Error
func (e *NumError) Error() string
func (*NumError) Unwrap
func (e *NumError) Unwrap() error