参考资料:
http://c.biancheng.net/view/vip_7305.html
https://vimsky.com/examples/list/code-usage-page-1.html
标准库中文文档:
https://studygolang.com/pkgdoc
实现了基本数据类型与其字符串表示的转换,主要有以下常用函数:
等价于ParseInt(s,10,0);
两个返回值
// Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
func Atoi(s string) (int, error)
func tAtoi() {
var s string = "100"
i, err := strconv.Atoi(s)
if err != nil {
fmt.Println("can't convert to int!,", err)
} else {
fmt.Printf("type:%T value:%#v\n", i, i)
}
}
//结果
type:int value:100
//转换失败:s="10a0"
can't convert to int!, strconv.Atoi: parsing "10a0": invalid syntax
等价于FormatInt(int64(i), 10);
只有一个结果返回值
// Itoa is equivalent to FormatInt(int64(i), 10).
func Itoa(i int) string {
return FormatInt(int64(i), 10)
}
func tItoA() {
var i int = 100
s := strconv.Itoa(i)
fmt.Printf("type:%T value:%#v\n", s, s)
}
//结果
type:string value:"100"
Parse类函数用于转换字符串为给定类型的值:ParseBool()、ParseInt()、ParseUint()、ParseFloat()。
返回字符串表示的bool值。它接受 “1”, “t”, “T”, “true”, “TRUE”, “True”,“0”, “f”, “F”, “false”, “FALSE”, “False”;否则返回错误。
// ParseBool returns the boolean value represented by the string.
// It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
// Any other value returns an error.
func ParseBool(str string) (bool, error) {
switch str {
case "1", "t", "T", "true", "TRUE", "True":
return true, nil
case "0", "f", "F", "false", "FALSE", "False":
return false, nil
}
return false, syntaxError("ParseBool", str)
}
func ParseInt(s string, base int, bitSize int) (i int64, err error)
func tParseInt() {
str := "-11"
i, err := strconv.ParseInt(str, 10, 0)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("type:%T value:%#v\n", i, i)
}
i2, err2 := strconv.ParseInt(str, 8, 0)//str是8进制的,结果永远是10进制的结果
if err != nil {
fmt.Println(err2)
} else {
fmt.Printf("type:%T value:%#v\n", i2, i2)
}
}
//结果
type:int64 value:-11
type:int64 value:-9
// ParseUint is like ParseInt but for unsigned numbers.
//
// A sign prefix is not permitted.
func ParseUint(s string, base int, bitSize int) (uint64, error)
func ParseFloat(s string, bitSize int) (float64, error) {
f, n, err := parseFloatPrefix(s, bitSize)
if n != len(s) && (err == nil || err.(*NumError).Err != ErrSyntax) {
return 0, syntaxError(fnParseFloat, s)
}
return f, err
}
func tParseFloat() {
str := "3.1415926"
fl, err := strconv.ParseFloat(str, 64)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("type:%T value:%#v\n", fl, fl)
}
{
str := "NaN"
fl, err := strconv.ParseFloat(str, 64)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("type:%T value:%#v\n", fl, fl)
}
}
}
//结果
type:float64 value:3.1415926
type:float64 value:NaN
// FormatBool returns "true" or "false" according to the value of b.
func FormatBool(b bool) string {
if b {
return "true"
}
return "false"
}
// FormatInt returns the string representation of i in the given base,
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
// for digit values >= 10.
func FormatInt(i int64, base int) string {
if fastSmalls && 0 <= i && i < nSmalls && base == 10 {
return small(int(i))
}
_, s := formatBits(nil, uint64(i), base, i < 0, false)
return s
}
func tFormatInt() {
var i int64 = 10
s := strconv.FormatInt(i, 2)
fmt.Printf("type:%T value:%#v\n", s, s)
{
s := strconv.FormatInt(i, 16)
fmt.Printf("type:%T value:%#v\n", s, s)
}
}
//结果
type:string value:"1010"
type:string value:"a"
// FormatUint returns the string representation of i in the given base,
// for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
// for digit values >= 10.
func FormatUint(i uint64, base int) string {
if fastSmalls && i < nSmalls && base == 10 {
return small(int(i))
}
_, s := formatBits(nil, i, base, false, false)
return s
}
func FormatFloat(f float64, fmt byte, prec, bitSize int) string {
return string(genericFtoa(make([]byte, 0, max(prec+4, 24)), f, fmt, prec, bitSize))
}
func tFormatFloat() {
var num float64 = 123.1415
strb := strconv.FormatFloat(num, 'b', -1, 64)
fmt.Printf("strb type:%T,value:%v\n ", strb, strb)
stre := strconv.FormatFloat(num, 'e', 7, 64)
fmt.Printf("stre type:%T,value:%v\n ", stre, stre)
strE := strconv.FormatFloat(num, 'E', 6, 64)
fmt.Printf("strE type:%T,value:%v\n ", strE, strE)
strf := strconv.FormatFloat(num, 'f', -1, 64)
fmt.Printf("strf type:%T,value:%v\n ", strf, strf)
strg := strconv.FormatFloat(num, 'g', 7, 64)
fmt.Printf("strg type:%T,value:%v\n ", strg, strg)
strG := strconv.FormatFloat(num, 'G', 6, 64)
fmt.Printf("strG type:%T,value:%v\n ", strG, strG)
strx := strconv.FormatFloat(num, 'x', 7, 64)
fmt.Printf("strx type:%T,value:%v\n ", strx, strx)
strX := strconv.FormatFloat(num, 'X', 6, 64)
fmt.Printf("strX type:%T,value:%v\n ", strX, strX)
}
//结果
strb type:string,value:8665312711153811p-46
stre type:string,value:1.2314150e+02
strE type:string,value:1.231415E+02
strf type:string,value:123.1415
strg type:string,value:123.1415
strG type:string,value:123.141
strx type:string,value:0x1.ec90e56p+06
strX type:string,value:0X1.EC90E5P+06
下面举了一个AppendInt()的例子
// AppendInt appends the string form of the integer i,
// as generated by FormatInt, to dst and returns the extended buffer.
func AppendInt(dst []byte, i int64, base int) []byte
func tAppendInt() {
// 声明一个slice
b10 := []byte("int (base 10):")
// 将转换为10进制的string,追加到slice中
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 IsPrint(r rune) bool
func main() {
fmt.Println(strconv.IsPrint('a'))
fmt.Println(strconv.IsPrint('\n'))
fmt.Println(strconv.IsPrint('♥'))
}
//结果
true
false
true
// IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such
// characters include letters, marks, numbers, punctuation, symbols, and
// spaces, from categories L, M, N, P, S, and Zs.
func IsGraphic(r rune) bool
func main() {
fmt.Println(strconv.IsGraphic('a'))
fmt.Println(strconv.IsGraphic('\n'))
fmt.Println(strconv.IsGraphic('♥'))
}
//结果
true
false
true
// Quote returns a double-quoted Go string literal representing s. The
// returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
// control characters and non-printable characters as defined by
// IsPrint.
func Quote(s string) string {
return quoteWith(s, '"', false, false)
}
func main() {
fmt.Println(strconv.Quote("Hello World!"))
fmt.Println(strconv.Quote(`" Hello
World! "`))
}
//结果
"Hellow World!"
"\" Hello\n\t World! \""
// QuoteRune returns a single-quoted Go character literal representing the
// rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
// for control characters and non-printable characters as defined by IsPrint.
// If r is not a valid Unicode code point, it is interpreted as the Unicode
// replacement character U+FFFD.
func QuoteRune(r rune) string {
return quoteRuneWith(r, '\'', false, false)
}
func main() {
if strconv.IsPrint(rune(65)) {
fmt.Println(strconv.QuoteRune(rune(65))) //A
}
if strconv.IsPrint(rune(10)) {
fmt.Println(strconv.QuoteRune(rune(10))) //换行
}
fmt.Println(strconv.QuoteRune('♥'))
fmt.Println(strconv.QuoteRune('s'))
}
//结果
'A'
'♥'
's'
//单独执行fmt.Println(strconv.QuoteRune(rune(10)))结果
'\n'
func QuoteRuneToASCII(r rune) string {
return quoteRuneWith(r, '\'', true, false)
}
func main() {
if strconv.IsPrint(rune(65)) {
fmt.Println(strconv.QuoteRuneToASCII(rune(65))) //A
}
if strconv.IsPrint(rune(10)) {
fmt.Println(strconv.QuoteRuneToASCII(rune(10))) //换行
}
fmt.Println(strconv.QuoteRuneToASCII('♥'))
fmt.Println(strconv.QuoteRuneToASCII('s'))
fmt.Println(strconv.QuoteRuneToASCII(rune(65555555)))
}
//结果
'A'
'\u2665'
's'
'\ufffd'
//持续更新…