目录
1. 字符串与整数的转换
2. 字符串与浮点数的转换
3. 布尔值的转换
4. 字符串的转义
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", '世') |
方法功能详解
-
字符串与整数的转换
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
进制的字符串。
-
字符串与浮点数的转换
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,表示结果的类型。
-
布尔值的转换
strconv.FormatBool(b bool)
:将布尔值b
转换为字符串 "true" 或 "false"。
-
字符串的转义
strconv.Quote(s string)
:返回带双引号的字符串,并转义内部的特殊字符(如 \n、\t 等)。这在需要确保字符串在编程语言语境下正确表示时很有用。strconv.QuoteRuneToASCII(r rune)
:返回单个字符的转义表示,仅使用 ASCII 字符。对于 Unicode 字符,返回其十六进制转义形式,如\U0001F60A
。
-
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
}