package main
import (
"fmt"
"strconv"
)
func main() {
//boolMain()
//complexMain()
//floatMain()
//intMain()
//unitMain()
quoteMain()
}
func boolMain() {
// 接受特定的 string 转 bool
fmt.Println(strconv.ParseBool("t"))
// 布尔转 string
fmt.Println(strconv.FormatBool(true))
// 将 bool 添加到缓冲区
fmt.Println(strconv.AppendBool([]byte(""),true))
}
// 复数操作
func complexMain() {
// 将字符串s转换为复数,精度为 bitSize
fmt.Println(strconv.ParseComplex("6+5i",64))
// 将复数c转换为 形式(a+bi)的字符串,其中a和b是实部和虚部,
fmt.Println(strconv.FormatComplex(128,'f',-1,64))
}
// 浮点数操作
func floatMain() {
// 字符串转浮点数,精度为 bitSize
fmt.Println(strconv.ParseFloat("5.342",64))
// 将浮点数f转换为字符串,
fmt.Println(strconv.FormatFloat(3.1415, 'f', -1, 32))
// 将由FormatFloat生成的浮点数f、 的字符串形式追加到dst,并返回扩展缓冲区。
fmt.Println(strconv.AppendFloat([]byte(""),3.1415,'f',-1,32))
}
// 整数操作
func intMain() {
// 按照给定的数字基数 base,精度 bitSize,将字符串转 int64
fmt.Println(strconv.ParseInt("5555",10,64))
// 将FormatInt生成的整数i的字符串形式 追加到dst,并返回扩展缓冲区。
fmt.Println(strconv.AppendInt([]byte(""),int64(42),10))
// int64转十进制string
fmt.Println(strconv.FormatInt(int64(42),10))
// 字符串转整数
fmt.Println(strconv.Atoi("5555"))
// 整数转字符串
fmt.Println(strconv.Itoa(5555))
}
func unitMain() {
// 将FormatUint生成的无符号整数i的字符串形式 追加到dst,并返回扩展缓冲区。
fmt.Println(strconv.AppendUint([]byte(""),uint64(42),10))
// 按照给定的数字基数 base,精度 bitSize,将字符串转 unit64
fmt.Println(strconv.ParseUint("5555",10,64))
// uint64转十进制string
fmt.Println(strconv.FormatUint(uint64(42), 10))
}
// 转意操作
func quoteMain() {
// 转意字符串
fmt.Println(strconv.Quote("hello world your's are good"))
// 将转意字符追加到 dst 并返回缓冲区
fmt.Println(strconv.AppendQuote([]byte(""),"hello world your's are good"))
// 转义字符串为ASCII
fmt.Println(strconv.QuoteToASCII("hello world your's are good"))
// 将rune转义为ASCII并添加
fmt.Println(strconv.AppendQuoteRuneToASCII([]byte("rune (ascii):"), '☺'))
// 转义字符串为Unicode图形字符
fmt.Println(strconv.QuoteToGraphic("jsdjfkh"))
// 将rune转义为Unicode图形字符并添加
fmt.Println(strconv.AppendQuoteToGraphic([]byte("rune (ascii):"), "☺"))
// rune 转 string
fmt.Println(strconv.QuoteRune('☺'))
// rune 转义 并添加
fmt.Println(strconv.AppendQuoteRune([]byte(""),'☺'))
// rune转ASCII
fmt.Println(strconv.QuoteRuneToASCII('☺'))
// rune转ASCII 并添加
fmt.Println(strconv.AppendQuoteRuneToASCII([]byte(""),'☺'))
// rune 转 Unicode
fmt.Println(strconv.QuoteRuneToGraphic('☺'))
// rune 转 Unicode 并添加
fmt.Println(strconv.AppendQuoteRuneToGraphic([]byte(""),'☺'))
// 判断是否可以转义
fmt.Println(strconv.CanBackquote(`"Fran & Freddie's Diner ☺"`))
// 反转义
fmt.Println(strconv.UnquoteChar(`\"Fran & Freddie's Diner\"`, '"'))
// 返回s前缀处的带引号的字符串
fmt.Println(strconv.QuotedPrefix(`"Fran & Freddie's Diner ☺"`))
// 返回s引号中的字符串值
fmt.Println(strconv.Unquote(`"Fran & Freddie's Diner ☺"`))
// 判断rune是否可以转义
fmt.Println(strconv.IsPrint('☺'))
// 判断rune是否可以转义为 Unicode
fmt.Println(strconv.IsGraphic('☺'))
}
go源码库学习之strconv库
最新推荐文章于 2024-05-11 14:33:08 发布