日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Go的strconv二

發(fā)布時(shí)間:2024/9/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Go的strconv二 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ParseBool 將字符串轉(zhuǎn)換為布爾值

func main() {fmt.Println(strconv.ParseBool("1")) // truefmt.Println(strconv.ParseBool("t")) // truefmt.Println(strconv.ParseBool("T")) // truefmt.Println(strconv.ParseBool("true")) // truefmt.Println(strconv.ParseBool("True")) // truefmt.Println(strconv.ParseBool("TRUE")) // truefmt.Println(strconv.ParseBool("TRue"))// false strconv.ParseBool: parsing "TRue": invalid syntaxfmt.Println(strconv.ParseBool("0")) // falsefmt.Println(strconv.ParseBool("f")) // falsefmt.Println(strconv.ParseBool("F")) // falsefmt.Println(strconv.ParseBool("false")) // falsefmt.Println(strconv.ParseBool("False")) // falsefmt.Println(strconv.ParseBool("FALSE")) // falsefmt.Println(strconv.ParseBool("FALse"))// false strconv.ParseBool: parsing "FAlse": invalid syntax }

FormatBool 將布爾值轉(zhuǎn)換為字符串 "true" 或 "false"

func main() {fmt.Println(strconv.FormatBool(0 < 1)) // truefmt.Println(strconv.FormatBool(0 > 1)) // false }

AppendBool?

// AppendBool 將布爾值 b 轉(zhuǎn)換為字符串 "true" 或 "false" // 然后將結(jié)果追加到 dst 的尾部,返回追加后的 []byte func main() {rst := make([]byte, 0)rst = strconv.AppendBool(rst, 0 < 1)fmt.Printf("%s\n", rst) // truerst = strconv.AppendBool(rst, 0 > 1)fmt.Printf("%s\n", rst) // false }

ParseFloat 將字符串轉(zhuǎn)換為浮點(diǎn)數(shù)

func ParseFloat(s string, bitSize int) (f float64, err error) bitSize:指定浮點(diǎn)類型(32:float32 64:float64) func main() {s := "0.12345678901234567890"f, err := strconv.ParseFloat(s, 32)fmt.Println(f, err) // 0.12345679104328156fmt.Println(float32(f), err) // 0.12345679f, err = strconv.ParseFloat(s, 64)fmt.Println(f, err) // 0.12345678901234568 }

ParseInt 將字符串轉(zhuǎn)換為 int 類型

// s: 要轉(zhuǎn)換的字符串 // base: 進(jìn)位制(2 進(jìn)制到 36 進(jìn)制) // bitSize:指定整數(shù)類型(0:int、8:int8、16:int16、32:int32、64:int64) // func ParseInt(s string, base int, bitSize int) (i int64, err error)func main() {fmt.Println(strconv.ParseInt("123", 10, 8))// 123fmt.Println(strconv.ParseInt("12345", 10, 8))// 127 strconv.ParseInt: parsing "12345": value out of rangefmt.Println(strconv.ParseInt("2147483647", 10, 0))// 2147483647fmt.Println(strconv.ParseInt("0xFF", 16, 0))// 0 strconv.ParseInt: parsing "0xFF": invalid syntaxfmt.Println(strconv.ParseInt("FF", 16, 0))// 255fmt.Println(strconv.ParseInt("0xFF", 0, 0))// 255 }

ParseUint

//功能同 ParseInt 一樣,只不過返回 uint 類型整數(shù)
func ParseUint(s string, base int, bitSize int) (n uint64, err error)

Atoi

相當(dāng)于 ParseInt(s, 10, 0)

func Atoi(s string) (i int, err error)func main() {fmt.Println(strconv.Atoi("2147483647"))// 2147483647fmt.Println(strconv.Atoi("2147483648"))// 2147483647 strconv.ParseInt: parsing "2147483648": value out of range }

FormatFloat

// FormatFloat 將浮點(diǎn)數(shù) f 轉(zhuǎn)換為字符串值 // f:要轉(zhuǎn)換的浮點(diǎn)數(shù) // fmt:格式標(biāo)記(b、e、E、f、g、G) // prec:精度(數(shù)字部分的長(zhǎng)度,不包括指數(shù)部分) // bitSize:指定浮點(diǎn)類型(32:float32、64:float64) // // 格式標(biāo)記: // 'b' (-ddddp±ddd,二進(jìn)制指數(shù)) // 'e' (-d.dddde±dd,十進(jìn)制指數(shù)) // 'E' (-d.ddddE±dd,十進(jìn)制指數(shù)) // 'f' (-ddd.dddd,沒有指數(shù)) // 'g' ('e':大指數(shù),'f':其它情況) // 'G' ('E':大指數(shù),'f':其它情況) // // 如果格式標(biāo)記為 'e','E'和'f',則 prec 表示小數(shù)點(diǎn)后的數(shù)字位數(shù) // 如果格式標(biāo)記為 'g','G',則 prec 表示總的數(shù)字位數(shù)(整數(shù)部分+小數(shù)部分)func FormatFloat(f float64, fmt byte, prec, bitSize int) stringfunc main() {f := 100.12345678901234567890123456789fmt.Println(strconv.FormatFloat(f, 'b', 5, 32))// 13123382p-17fmt.Println(strconv.FormatFloat(f, 'e', 5, 32))// 1.00123e+02fmt.Println(strconv.FormatFloat(f, 'E', 5, 32))// 1.00123E+02fmt.Println(strconv.FormatFloat(f, 'f', 5, 32))// 100.12346fmt.Println(strconv.FormatFloat(f, 'g', 5, 32))// 100.12fmt.Println(strconv.FormatFloat(f, 'G', 5, 32))// 100.12fmt.Println(strconv.FormatFloat(f, 'b', 30, 32))// 13123382p-17fmt.Println(strconv.FormatFloat(f, 'e', 30, 32))// 1.001234588623046875000000000000e+02fmt.Println(strconv.FormatFloat(f, 'E', 30, 32))// 1.001234588623046875000000000000E+02fmt.Println(strconv.FormatFloat(f, 'f', 30, 32))// 100.123458862304687500000000000000fmt.Println(strconv.FormatFloat(f, 'g', 30, 32))// 100.1234588623046875fmt.Println(strconv.FormatFloat(f, 'G', 30, 32))// 100.1234588623046875 }

AppendFloat

//將浮點(diǎn)數(shù) f 轉(zhuǎn)換為字符串值,并將轉(zhuǎn)換結(jié)果追加到 dst 的尾部 //返回追加后的 []byte //func AppendFloat(dst []byte, f float64, fmt byte, prec int, bitSize int) []bytefunc main() {f := 100.12345678901234567890123456789b := make([]byte, 0)b = strconv.AppendFloat(b, f, 'f', 5, 32)b = append(b, " "...)b = strconv.AppendFloat(b, f, 'e', 5, 32)fmt.Printf("%s", b) // 100.12346 1.00123e+0 }

FormatInt?

// 將 int 型整數(shù) i 轉(zhuǎn)換為字符串形式 // base:進(jìn)位制(2 進(jìn)制到 36 進(jìn)制) // 大于 10 進(jìn)制的數(shù),返回值使用小寫字母 'a' 到 'z' // func FormatInt(i int64, base int) stringfunc main() {i := int64(-2048)fmt.Println(strconv.FormatInt(i, 2)) // -100000000000fmt.Println(strconv.FormatInt(i, 8)) // -4000fmt.Println(strconv.FormatInt(i, 10)) // -2048fmt.Println(strconv.FormatInt(i, 16)) // -800fmt.Println(strconv.FormatInt(i, 36)) // -1kw }

FormmaUint

// FormatUint 將 uint 型整數(shù) i 轉(zhuǎn)換為字符串形式 // base:進(jìn)位制(2 進(jìn)制到 36 進(jìn)制) // 大于 10 進(jìn)制的數(shù),返回值使用小寫字母 'a' 到 'z' func FormatUint(i uint64, base int) string

ITOA

Itoa 相當(dāng)于 FormatInt(i, 10) func Itoa(i int) stringfunc main() {fmt.Println(strconv.Itoa(-2048)) // -2048fmt.Println(strconv.Itoa(2048)) // 2048 }

AppendInt?

// AppendInt 將 int 型整數(shù) i 轉(zhuǎn)換為字符串形式,并追加到 dst 的尾部 // i:要轉(zhuǎn)換的字符串 // base:進(jìn)位制 // 返回追加后的 []byte func AppendInt(dst []byte, i int64, base int) []bytefunc main() {b := make([]byte, 0)b = strconv.AppendInt(b, -2048, 16)fmt.Printf("%s", b) // -800 }

AppendUint?

// AppendUint 將 uint 型整數(shù) i 轉(zhuǎn)換為字符串形式,并追加到 dst 的尾部 // i:要轉(zhuǎn)換的字符串 // base:進(jìn)位制 // 返回追加后的 []byte func AppendUint(dst []byte, i uint64, base int) []byte

Quote?

// Quote 將字符串 s 轉(zhuǎn)換為“雙引號(hào)”引起來的字符串 // 其中的特殊字符將被轉(zhuǎn)換為“轉(zhuǎn)義字符” // “不可顯示的字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符” func Quote(s string) string func main() {fmt.Println(strconv.Quote(`C:\Windows`))// "C:\\Windows" }

AppendQuote?

// AppendQuote 將字符串 s 轉(zhuǎn)換為“雙引號(hào)”引起來的字符串, // 并將結(jié)果追加到 dst 的尾部,返回追加后的 []byte // 其中的特殊字符將被轉(zhuǎn)換為“轉(zhuǎn)義字符” func AppendQuote(dst []byte, s string) []byte func main() {s := `C:\Windows`b := make([]byte, 0)b = strconv.AppendQuote(b, s)fmt.Printf("%s", b) // "C:\\Windows" }

QuoteToASCII?

// QuoteToASCII 將字符串 s 轉(zhuǎn)換為“雙引號(hào)”引起來的 ASCII 字符串 // “非 ASCII 字符”和“特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符” func QuoteToASCII(s string) stringfunc main() {fmt.Println(strconv.QuoteToASCII("Hello 世界!"))// "Hello \u4e16\u754c\uff01" }

AppendQuoteToASCII?

// AppendQuoteToASCII 將字符串 s 轉(zhuǎn)換為“雙引號(hào)”引起來的 ASCII 字符串, // 并將結(jié)果追加到 dst 的尾部,返回追加后的 []byte // “非 ASCII 字符”和“特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符” func AppendQuoteToASCII(dst []byte, s string) []bytefunc main() {s := "Hello 世界!"b := make([]byte, 0)b = strconv.AppendQuoteToASCII(b, s)fmt.Printf("%s", b) // "Hello \u4e16\u754c\uff01" }

QuoteRune?

// QuoteRune 將 Unicode 字符轉(zhuǎn)換為“單引號(hào)”引起來的字符串 // “特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符” func QuoteRune(r rune) string func main() {fmt.Println(strconv.QuoteRune('好'))// '好' }

AppendQuoteRune?

// AppendQuoteRune 將 Unicode 字符轉(zhuǎn)換為“單引號(hào)”引起來的字符串, // 并將結(jié)果追加到 dst 的尾部,返回追加后的 []byte // “特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符” func AppendQuoteRune(dst []byte, r rune) []byte func main() {b := make([]byte, 0)b = strconv.AppendQuoteRune(b, '好')fmt.Printf("%s", b) // '好' }

QuoteRuneToASCII?

// QuoteRuneToASCII 將 Unicode 字符轉(zhuǎn)換為“單引號(hào)”引起來的 ASCII 字符串 // “非 ASCII 字符”和“特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符” func QuoteRuneToASCII(r rune) string func main() {fmt.Println(strconv.QuoteRuneToASCII('好'))// '\u597d' }

AppendQuoteRune?

// AppendQuoteRune 將 Unicode 字符轉(zhuǎn)換為“單引號(hào)”引起來的 ASCII 字符串, // 并將結(jié)果追加到 dst 的尾部,返回追加后的 []byte // “非 ASCII 字符”和“特殊字符”將被轉(zhuǎn)換為“轉(zhuǎn)義字符” func AppendQuoteRuneToASCII(dst []byte, r rune) []bytefunc main() {b := make([]byte, 0)b = strconv.AppendQuoteRuneToASCII(b, '好')fmt.Printf("%s", b) // '\u597d' }

CanBackquote?

// CanBackquote 判斷字符串 s 是否可以表示為一個(gè)單行的“反引號(hào)”字符串 // 字符串中不能含有控制字符(除了 \t)和“反引號(hào)”字符,否則返回 false func CanBackquote(s string) bool func main() {b := strconv.CanBackquote("C:\\Windows\n")fmt.Println(b) // falseb = strconv.CanBackquote("C:\\Windows\r")fmt.Println(b) // falseb = strconv.CanBackquote("C:\\Windows\f")fmt.Println(b) // falseb = strconv.CanBackquote("C:\\Windows\t")fmt.Println(b) // trueb = strconv.CanBackquote("C:\\`Windows`")fmt.Println(b) // false }

UnquoteChar?

// UnquoteChar 將 s 中的第一個(gè)字符“取消轉(zhuǎn)義”并解碼 // // s:轉(zhuǎn)義后的字符串 // quote:字符串使用的“引號(hào)符”(用于對(duì)引號(hào)符“取消轉(zhuǎn)義”) // // value: 解碼后的字符 // multibyte:value 是否為多字節(jié)字符 // tail: 字符串 s 除去 value 后的剩余部分 // error: 返回 s 中是否存在語(yǔ)法錯(cuò)誤 // // 參數(shù) quote 為“引號(hào)符” // 如果設(shè)置為單引號(hào),則 s 中允許出現(xiàn) \' 字符,不允許出現(xiàn)單獨(dú)的 ' 字符 // 如果設(shè)置為雙引號(hào),則 s 中允許出現(xiàn) \" 字符,不允許出現(xiàn)單獨(dú)的 " 字符 // 如果設(shè)置為 0,則不允許出現(xiàn) \' 或 \" 字符,可以出現(xiàn)單獨(dú)的 ' 或 " 字符 func UnquoteChar(s string, quote byte) (value rune, multibyte bool, tail string, err error)func main() {s := `\"大\\家\\好!\"`c, mb, sr, _ := strconv.UnquoteChar(s, '"')fmt.Printf("%-3c %v\n", c, mb)for ; len(sr) > 0; c, mb, sr, _ = strconv.UnquoteChar(sr, '"') {fmt.Printf("%-3c %v\n", c, mb)}// " false// 大 true// \ false// 家 true// \ false// 好 true// ! true }

Unquote?

// Unquote 將“帶引號(hào)的字符串” s 轉(zhuǎn)換為常規(guī)的字符串(不帶引號(hào)和轉(zhuǎn)義字符) // s 可以是“單引號(hào)”、“雙引號(hào)”或“反引號(hào)”引起來的字符串(包括引號(hào)本身) // 如果 s 是單引號(hào)引起來的字符串,則返回該該字符串代表的字符 func Unquote(s string) (t string, err error)func main() {sr, err := strconv.Unquote(`"\"大\t家\t好!\""`)fmt.Println(sr, err)sr, err = strconv.Unquote(`'大家好!'`)fmt.Println(sr, err)sr, err = strconv.Unquote(`'好'`)fmt.Println(sr, err)sr, err = strconv.Unquote("`大\\t家\\t好!`")fmt.Println(sr, err) }

IsPrint?

// IsPrint 判斷 Unicode 字符 r 是否是一個(gè)可顯示的字符 // 可否顯示并不是你想象的那樣,比如空格可以顯示,而\t則不能顯示 // 具體可以參考 Go 語(yǔ)言的源碼 func IsPrint(r rune) boolfunc main() {fmt.Println(strconv.IsPrint('a')) // truefmt.Println(strconv.IsPrint('好')) // truefmt.Println(strconv.IsPrint(' ')) // truefmt.Println(strconv.IsPrint('\t')) // falsefmt.Println(strconv.IsPrint('\n')) // falsefmt.Println(strconv.IsPrint(0)) // false }

?

總結(jié)

以上是生活随笔為你收集整理的Go的strconv二的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。