Golang 中的 Strconv 包详解,看这篇就够了

2023年 11月 3日 68.9k 0

strconv 是 Golang 中一个非常常用的包,主要用于字符串和基本数据类型之间的相互转换。本文将详细介绍 strconv 包的常用函数及用法。

strconv.Atoi 和 strconv.Itoa

Atoi 函数用于将字符串转换为 int 类型,Itoa 函数则用于将 int 类型转换为字符串类型。简单使用示例如下:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    str := "123"
    intValue, _ := strconv.Atoi(str)
    fmt.Printf("str to int: %dn", intValue)

    intValue += 1
    str = strconv.Itoa(intValue)
    fmt.Printf("int to str: %sn", str)
}

strconv.Parse 系列函数

strconv.Parse 系列函数用于将字符串解析为指定类型。其中常用的函数有 ParseInt、ParseBool 和 ParseFloat。简单使用示例如下:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	// 解析整数
	intStr := "123"
	intValue, _ := strconv.ParseInt(intStr, 10, 64)
	fmt.Printf("Parsed int value: %dn", intValue)

	// 解析布尔值
	boolStr := "true"
	boolValue, _ := strconv.ParseBool(boolStr)
	fmt.Printf("Parsed bool value: %tn", boolValue)

	// 解析浮点数
	floatStr := "3.14"
	floatValue, _ := strconv.ParseFloat(floatStr, 64)
	fmt.Printf("Parsed float value: %fn", floatValue)
}

strconv.Format 系列函数

strconv.Format 系列函数用于将基本数据类型转换为字符串类型。常用的函数有 FormatInt、FormatBool 和 FormatFloat。简单使用示例如下:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	// 格式化整数
	intValue := 123
	intStr := strconv.FormatInt(int64(intValue), 10)
	fmt.Printf("Formatted int string: %sn", intStr)

	// 格式化布尔值
	boolValue := true
	boolStr := strconv.FormatBool(boolValue)
	fmt.Printf("Formatted bool string: %sn", boolStr)

	// 格式化浮点数
	floatValue := 3.14
	floatStr := strconv.FormatFloat(floatValue, 'f', -1, 64)
	fmt.Printf("Formatted float string: %sn", floatStr)
}

strconv.Append 系列函数

strconv.Append 系列函数用于将基本数据类型追加到已存在的字节数组中。常用的函数有 AppendInt、AppendBool 和 AppendFloat。简单使用示例如下:

package main

import (
    "fmt"
    "strconv"
)

func main() {
    // 追加整数到字节数组
    num1 := 123
    byteSlice := []byte("Number: ")
    byteSlice = strconv.AppendInt(byteSlice, int64(num1), 10)
    fmt.Printf("Appended int: %sn", byteSlice)

    // 追加布尔值到字节数组
    boolVal := true
    byteSlice = []byte("Bool: ")
    byteSlice = strconv.AppendBool(byteSlice, boolVal)
    fmt.Printf("Appended bool: %sn", byteSlice)

    // 追加浮点数到字节数组
    floatVal := 3.14
    byteSlice = []byte("Float: ")
    byteSlice = strconv.AppendFloat(byteSlice, floatVal, 'f', -1, 64)
    fmt.Printf("Appended float: %sn", byteSlice)
}

strconv.IsPrint 和 strconv.IsGraphic

strconv.IsPrint 函数用于判断给定的 Unicode 字符是否可打印,可打印字符是指那些可以在屏幕上显示的字符。strconv.IsGraphic 函数用于判断给定的 Unicode 字符是否是图形字符,图形字符是指可视化的字符。简单使用示例如下:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	chars := []rune{'H', 'e', 'l', 'n', '♥', 127}
	for _, char := range chars {
		fmt.Printf("Character: %c, IsPrint: %vn", char, strconv.IsPrint(char))
		fmt.Printf("Character: %c, IsGraphic: %vn", char, strconv.IsGraphic(char))
	}
}

strconv.Quote 和 strconv.Unquote 系列函数

strconv.Quote 系列函数用于转义和引用字符串的功能,将字符串转换为可以直接表示的字符串字面值(literal),包括添加转义字符和引号。简单使用示例如下:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	str := `路多辛的, "所思所想"!`

	quoted := strconv.Quote(str)
	fmt.Println("Quoted: ", quoted)

	unquoted, err := strconv.Unquote(quoted)
	if err != nil {
		fmt.Println("Unquote error: ", err)
	} else {
		fmt.Println("Unquoted: ", unquoted)
	}
}

strconv.CanBackquote

strconv.CanBackquote 函数用于检查字符串是否可以不变地表示为单行反引号字符串(即以 `` 开头和结尾的字符串)。简单使用示例如下:

package main

import (
	"fmt"
	"strconv"
)

func main() {
	str1 := "Hello, world!"
	str2 := "`Hello, world!`"
	str3 := "`Hello,nworld!`"

	fmt.Println(strconv.CanBackquote(str1)) // 输出:false
	fmt.Println(strconv.CanBackquote(str2)) // 输出:true
	fmt.Println(strconv.CanBackquote(str3)) // 输出:false
}

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论