go源码库学习之strconv库

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('☺'))
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值