- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
第
Golang?中的?strconv?包常用函数及用法详解
目录strconv.Atoi和strconv.Itoastrconv.Parse系列函数strconv.Format系列函数strconv.Append系列函数strconv.IsPrint和strconv.IsGraphicstrconv.Quote和strconv.Unquote系列函数strconv.CanBackquotestrconv是Golang中一个非常常用的包,主要用于字符串和基本数据类型之间的相互转换。本文将详细介绍strconv包的常用函数及用法。
strconv.Atoi和strconv.Itoa
Atoi函数用于将字符串转换为int类型,Itoa函数则用于将int类型转换为字符串类型。简单使用示例如下:
packagemain
import(
fmt
strconv
funcmain(){
str:=123
intValue,_:=strconv.Atoi(str)
fmt.Printf(strtoint:%d\n,intValue)
intValue+=1
str=strconv.Itoa(intValue)
fmt.Printf(inttostr:%s\n,str)
}
strconv.Parse系列函数
strconv.Parse系列函数用于将字符串解析为指定类型。其中常用的函数有ParseInt、ParseBool和ParseFloat。简单使用示例如下:
packagemain
import(
fmt
strconv
funcmain(){
//解析整数
intStr:=123
intValue,_:=strconv.ParseInt(intStr,10,64)
fmt.Printf(Parsedintvalue:%d\n,intValue)
//解析布尔值
boolStr:=true
boolValue,_:=strconv.ParseBool(boolStr)
fmt.Printf(Parsedboolvalue:%t\n,boolValue)
//解析浮点数
floatStr:=3.14
floatValue,_:=strconv.ParseFloat(floatStr,64)
fmt.Printf(Parsedfloatvalue:%f\n,floatValue)
}
strconv.Format系列函数
strconv.Format系列函数用于将基本数据类型转换为字符串类型。常用的函数有FormatInt、FormatBool和FormatFloat。简单使用示例如下:
packagemain
import(
fmt
strconv
funcmain(){
//格式化整数
intValue:=123
intStr:=strconv.FormatInt(int64(intValue),10)
fmt.Printf(Formattedintstring:%s\n,intStr)
//格式化布尔值
boolValue:=true
boolStr:=strconv.FormatBool(boolValue)
fmt.Printf(Formattedboolstring:%s\n,boolStr)
//格式化浮点数
floatValue:=3.14
floatStr:=strconv.FormatFloat(floatValue,f,-1,64)
fmt.Printf(Formattedfloatstring:%s\n,floatStr)
}
strconv.Append系列函数
strconv.Append系列函数用于将基本数据类型追加到已存在的字节数组中。常用的函数有AppendInt、AppendBool和AppendFloat。简单使用示例如下:
packagemain
import(
fmt
strconv
funcmain(){
//追加整数到字节数组
num1:=123
byteSlice:=[]byte(Number:)
文档评论(0)