golang 的strconv包常用方法

目录

[1. 字符串与整数的转换](#1. 字符串与整数的转换)

[2. 字符串与浮点数的转换](#2. 字符串与浮点数的转换)

[3. 布尔值的转换](#3. 布尔值的转换)

[4. 字符串的转义](#4. 字符串的转义)

[5. 补充:rune 类型的使用](#5. 补充:rune 类型的使用)

方法功能详解

代码示例:


1. 字符串与整数的转换
方法名称 功能描述 示例
Atoi 将字符串转换为十进制整数。 strconv.Atoi("123")
Itoa 将整数转换为十进制字符串。 strconv.Itoa(123)
ParseInt 将字符串解析为指定进制的整数。 strconv.ParseInt("520", 10, 64)
ParseUint 将字符串解析为指定进制的无符号整数。 strconv.ParseUint("FF", 16, 64)
FormatInt 将整数转换为指定进制的字符串。 strconv.FormatInt(520, 8)
FormatUint 将无符号整数转换为指定进制的字符串。 strconv.FormatUint(520, 8)
2. 字符串与浮点数的转换
方法名称 功能描述 示例
ParseFloat 将字符串解析为浮点数。 strconv.ParseFloat("3.1415926", 64)
FormatFloat 将浮点数转换为字符串,并格式化。 strconv.FormatFloat(3.1415926, 'f', 3, 32)
3. 布尔值的转换
方法名称 功能描述 示例
FormatBool 将布尔值转换为字符串 "true" 或 "false"。 strconv.FormatBool(true)
4. 字符串的转义
方法名称 功能描述 示例
Quote 返回带双引号的转义字符串。 strconv.Quote("hello world")
QuoteRuneToASCII 返回单个字符的转义表示,仅使用ASCII字符。 strconv.QuoteRuneToASCII('😊')
5. 补充:rune 类型的使用
方法名称 功能描述 示例
rune 表示单个 Unicode 码点。 var r rune = 'A'
FormatUint 转换为整数值查看 Unicode 码点。 fmt.Printf("Unicode code point of '世' is %U\n", '世')

方法功能详解

  1. 字符串与整数的转换

    • strconv.Atoi(s string) :将字符串 s 转换为十进制整数。如果转换成功,返回转换后的整数和 nil 错误;否则,返回 0 和非 nil 错误。
    • strconv.Itoa(i int) :将整数 i 转换为十进制字符串。
    • strconv.ParseInt(s string, base int, bitSize int) :将字符串 s 解析为指定 base 进制的整数,bitSize 参数可以是 0(表示整数是无符号的)、32 或 64,以指定结果的类型。如果 base 为 0,则自动检测进制(默认为十进制,但前缀为 "0x" 的字符串将被解释为十六进制)。
    • strconv.ParseUint(s string, base int, bitSize int) :功能与 ParseInt 类似,但用于无符号整数。
    • strconv.FormatInt(i int64, base int) :将整数 i 转换为指定 base 进制的字符串。
    • strconv.FormatUint(i uint64, base int) :将无符号整数 i 转换为指定 base 进制的字符串。
  2. 字符串与浮点数的转换

    • strconv.ParseFloat(s string, bitSize int) :将字符串 s 解析为浮点数,bitSize 为 32 或 64,以指定结果的类型(float32 或 float64)。
    • strconv.FormatFloat(f float64, format byte, precision, bitSize int) :
      • format:表示格式,可以是 'f'(定点)、'e'(科学计数)、'g'(根据情况选择 'e' 或 'f')。
      • precision:决定结果的小数位数或科学计数法的位数。
      • bitSize:可以是 32 或 64,表示结果的类型。
  3. 布尔值的转换

    • strconv.FormatBool(b bool) :将布尔值 b 转换为字符串 "true" 或 "false"。
  4. 字符串的转义

    • strconv.Quote(s string):返回带双引号的字符串,并转义内部的特殊字符(如 \n、\t 等)。这在需要确保字符串在编程语言语境下正确表示时很有用。
    • strconv.QuoteRuneToASCII(r rune) :返回单个字符的转义表示,仅使用 ASCII 字符。对于 Unicode 字符,返回其十六进制转义形式,如 \U0001F60A。
  5. rune 类型的使用

    • rune :是 Go 语言中用于表示单个 Unicode 码点的内置类型,可以直接赋值字符(如 var r rune = 'A')。
    • fmt.Printf("%U", r) :打印 Unicode 码点的完整表示,例如 U+0041 表示字符 'A'。

代码示例:

复制代码
package main

import (
	"fmt"
	"strconv"
)

func main() {
	n, _ := strconv.Atoi("123")
	fmt.Printf("%d\n", n)

	s1 := strconv.Itoa(123)
	fmt.Printf("%s\n", s1)

	// 字符串、进制、位数
	n2, _ := strconv.ParseInt("520", 10, 64)
	fmt.Printf("%d\n", n2)
	n3, _ := strconv.ParseUint("FF", 16, 64) // 将十六进制字符串"FF"转换为无符号整数
	fmt.Printf("%d\n", n3)

	//
	s2 := strconv.FormatBool(true)
	fmt.Println(s2) // "true"

	//
	s3 := strconv.FormatFloat(3.1415926, 'f', 3, 32)
	fmt.Println(s3)

	//
	s4 := strconv.FormatInt(520, 8)
	fmt.Println(s4)
	s4 = strconv.FormatUint(520, 8)
	fmt.Println(s4)

	//
	s5 := strconv.Quote("hello world")
	fmt.Println(s5) // `"hello world"` 返回带双引号的字符串

	//
	s6 := strconv.QuoteRuneToASCII('!') //单个rune
	fmt.Println(s6)                     // `'!'`

	var dst []byte
	dst = strconv.AppendQuoteRuneToASCII(dst, '😊')
	fmt.Println(string(dst)) // `\U0001F60A`

	// rune类型:rune 是一个内置类型,它是 int32 的别名,主要用于表示单个Unicode字符。
	// 这意味着 rune 可以存储任何Unicode码点(code point),包括所有的国际字符、符号以及表情符号等。
	// 直接定义一个rune变量
	var r rune = 'A'
	fmt.Printf("Type of r is %T and value is %c\n", r, r)
	// 遍历字符串中的rune
	str := "Hello, 世界"
	for _, r := range str {
		fmt.Printf("%c ", r) // 输出: H e l l o ,   世 界
	}
	fmt.Println()
	// 转换为整数值查看Unicode码点
	fmt.Printf("Unicode code point of '世' is %U\n", '世') // 输出: Unicode code point of '世' is U+4E16

}
相关推荐
小小龙学IT10 小时前
三菱 PLC MC 协议(SLMP / QnA 兼容 3E 帧)深度解析:从帧结构到 C++/Go 双语言采集实战
c语言·c++·golang
念何架构之路10 小时前
zap日志SugaredLogger 剖析
云原生·golang
FfHUCisI15 小时前
sync.Once 与 sync.Cond 源码与并发控制陷阱
服务器·开发语言·后端·golang
Wx-bishekaifayuan21 小时前
springboot生活商城系统21035-计算机课程设计、毕业设计
spring boot·后端·python·spring·elasticsearch·golang·课程设计
ttwuai7 天前
Go开源后台管理系统推荐:怎么按技术栈和边界比较4个官方仓库?
golang·gin
codeejun7 天前
每日一Go·MySQL-5、锁机制全解析
云原生·golang
Achou.Wang7 天前
k8s中nginx worker process自动设置
后端·golang
指尖的爷8 天前
ARM 架构 Ubuntu(RK3588/aarch64)go开发环境安装手册
ubuntu·架构·golang
名字还没想好☜8 天前
Go 实现指数退避重试:context 取消、抖动 jitter 与什么时候别重试
后端·golang·go
Casbin开源社区8 天前
OpenAgent 详解:单二进制自托管 AI Agent 平台,30+ 模型接入、RAG 知识库、MCP 工具调用与 Casbin 工具权限
人工智能·golang·开源