-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path第 04.07.02 章、strconv 包
128 lines (82 loc) · 4.28 KB
/
第 04.07.02 章、strconv 包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
strconv 包负责字符串与基本类型之间的互相转换
【补充内容 Unicode 码点(Code Point) 分类】
graphic characters 具有特定语义的字符,包括可见的字形和可见的空白字符
format characters 没有可以显示的外观,但可以用以修改临近其他字符的外观或作用
control code characters 控制符,比如 NUL BEL BS HT LF VT FF CR EOF ESC DEL 等
-------- 第一大类 常用字符数字转换 --------
strconv.Atoi(s string) (int, error)
用 strconv.ParseInt(s, 10, 0) 将 ASCII 字符转换为 10 进制的 int 型
strconv.Itoa(i int) string
用 strconv.FormatInt(int64(i), 10) 将 int64 类型以 10 为底转化为 string 类型
-------- 第二大类 字符串判断 --------
strconv.CanBackquote(s string) bool
检查一个字符串是否可以用一行字符串表示
且该行字符串不含有除 tab 之外的控制符
strconv.IsGraphic(r rune) bool
判断一个 Rune 是否是一个 graphic character
strconv.IsPrint(r rune) bool
判断一个 Rune 是否可以被打印出来
可打印的字符有:字母、数字、标点、符号和 ASCII 空格
-------- 第三大类 各类型转字符串 --------
strconv.FormatBool(b bool) string
boolean 转 string
strconv.FormatFloat(f float64, fmt byte, prec, bitSize int) string
float64 转 string
fmt: 转换为 string 所使用的格式,一共有 6 种,注意该参数接受 byte 类型
'b': -ddddp±ddd 二进制科学计数法
'e': -d.ddde±dd 十进制小写 e 科学计数法
'E': -d.dddE±dd 十进制大写 E 科学计数法
'f': -ddd.dddd 普通十进制小数写法
'g': 自动切换为 'e' 或 'f'
'G': 自动切换为 'E' 或 'f'
prec: 规定打印使用的位数(不包括指数的位数),该参数对 'b' 格式无效
对于 'e'/'E'/'f': 小数点后所使用的位数,对于 prec 小于 0 的数,使用最小可表示完整精度的位数表示
对于 'g'/'G': 整个数字(不算小数点)的位数,对于 prec = 0,等价于 prec = 1,对于 prec 小于 0 的数,使用最小可表示完整精度的位数表示
bitSize: 原始数据的精度,32 对应 float32,64 对应 float64
strconv.FormatInt(i int64, base int) string
int64 转 string,基于 base 指定的进制
使用小写 a-z 表示超过 10 的数
strconv.FormatUint(i uint64, base int) string
uint64 转 string,基于 base 指定的进制
使用小写 a-z 表示超过 10 的数
-------- 第四大类 字符串转各类型 --------
strconv.ParseBool(str string) (bool, error)
string 转 boolean
接受的字符串 1 t T TRUE true True 0 f F FALSE false False
strconv.ParseFloat(s string, bitSize int) (float64, error)
strconv.ParseInt(s string, base int, bitSize int) (i int64, err error)
strconv.ParseUint(s string, base int, bitSize int) (uint64, error)
string 转数字
原数据的进度为 bitSize,取值 32 或 64
原数据的进制为 base,若不定义,则以 0x 开头的为 16 进制,0 开头的为 8 进制,其他均为 10 进制
-------- 第五大类 字符串的安全表示和反转化 --------
strconv.Quote(s string) string
将字符串中的控制符和不可打印的符号转换为转义字符
并和两头的双引号一同输出为字符串
strconv.QuoteToASCII(s string) string
将字符串中非 ASCII 字符都变为转义字符
并和两头的双引号一同输出为字符串
strconv.QuoteToGraphic(s string) string
将字符串中非 Graphic 字符都变为转义字符
并和两头的双引号一同输出为字符串
strconv.QuoteRune(r rune) string
strconv.QuoteRuneToASCII(r rune) string
strconv.QuoteRuneToGraphic(r rune) string
同上,不同在于包含在内的是两头的单引号
strconv.Unquote(s string) (string, error)
将两头的引号从字符串中移除,显示除原本的文本内容
strconv.UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)
【用法不明】
-------- 第六大类 字节流后衔接各类型 --------
strconv.AppendBool(dst []byte, b bool) []byte
strconv.AppendFloat
strconv.AppendInt
strconv.AppendQuote
strconv.AppendQuoteRune
strconv.AppendQuoteRuneToASCII
strconv.AppendQuoteRuneToGraphic
strconv.AppendQuoteToASCII
strconv.AppendQuoteToGraphic
strconv.AppendUint
将各种数据添加至 []byte 之后
需要的参数为 原始字节流 []byte 以及对应的 Format/Quota 需要的参数