Go的strconv二
生活随笔
收集整理的這篇文章主要介紹了
Go的strconv二
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
ParseBool 將字符串轉換為布爾值
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 將布爾值轉換為字符串 "true" 或 "false"
func main() {fmt.Println(strconv.FormatBool(0 < 1)) // truefmt.Println(strconv.FormatBool(0 > 1)) // false }AppendBool?
// AppendBool 將布爾值 b 轉換為字符串 "true" 或 "false" // 然后將結果追加到 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 將字符串轉換為浮點數(shù)
func ParseFloat(s string, bitSize int) (f float64, err error) bitSize:指定浮點類型(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 將字符串轉換為 int 類型
// s: 要轉換的字符串 // base: 進位制(2 進制到 36 進制) // 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
相當于 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 將浮點數(shù) f 轉換為字符串值 // f:要轉換的浮點數(shù) // fmt:格式標記(b、e、E、f、g、G) // prec:精度(數(shù)字部分的長度,不包括指數(shù)部分) // bitSize:指定浮點類型(32:float32、64:float64) // // 格式標記: // 'b' (-ddddp±ddd,二進制指數(shù)) // 'e' (-d.dddde±dd,十進制指數(shù)) // 'E' (-d.ddddE±dd,十進制指數(shù)) // 'f' (-ddd.dddd,沒有指數(shù)) // 'g' ('e':大指數(shù),'f':其它情況) // 'G' ('E':大指數(shù),'f':其它情況) // // 如果格式標記為 'e','E'和'f',則 prec 表示小數(shù)點后的數(shù)字位數(shù) // 如果格式標記為 '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
//將浮點數(shù) f 轉換為字符串值,并將轉換結果追加到 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 轉換為字符串形式 // base:進位制(2 進制到 36 進制) // 大于 10 進制的數(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 轉換為字符串形式 // base:進位制(2 進制到 36 進制) // 大于 10 進制的數(shù),返回值使用小寫字母 'a' 到 'z' func FormatUint(i uint64, base int) stringITOA
Itoa 相當于 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 轉換為字符串形式,并追加到 dst 的尾部 // i:要轉換的字符串 // base:進位制 // 返回追加后的 []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 轉換為字符串形式,并追加到 dst 的尾部 // i:要轉換的字符串 // base:進位制 // 返回追加后的 []byte func AppendUint(dst []byte, i uint64, base int) []byteQuote?
// Quote 將字符串 s 轉換為“雙引號”引起來的字符串 // 其中的特殊字符將被轉換為“轉義字符” // “不可顯示的字符”將被轉換為“轉義字符” func Quote(s string) string func main() {fmt.Println(strconv.Quote(`C:\Windows`))// "C:\\Windows" }AppendQuote?
// AppendQuote 將字符串 s 轉換為“雙引號”引起來的字符串, // 并將結果追加到 dst 的尾部,返回追加后的 []byte // 其中的特殊字符將被轉換為“轉義字符” 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 轉換為“雙引號”引起來的 ASCII 字符串 // “非 ASCII 字符”和“特殊字符”將被轉換為“轉義字符” func QuoteToASCII(s string) stringfunc main() {fmt.Println(strconv.QuoteToASCII("Hello 世界!"))// "Hello \u4e16\u754c\uff01" }AppendQuoteToASCII?
// AppendQuoteToASCII 將字符串 s 轉換為“雙引號”引起來的 ASCII 字符串, // 并將結果追加到 dst 的尾部,返回追加后的 []byte // “非 ASCII 字符”和“特殊字符”將被轉換為“轉義字符” 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 字符轉換為“單引號”引起來的字符串 // “特殊字符”將被轉換為“轉義字符” func QuoteRune(r rune) string func main() {fmt.Println(strconv.QuoteRune('好'))// '好' }AppendQuoteRune?
// AppendQuoteRune 將 Unicode 字符轉換為“單引號”引起來的字符串, // 并將結果追加到 dst 的尾部,返回追加后的 []byte // “特殊字符”將被轉換為“轉義字符” func AppendQuoteRune(dst []byte, r rune) []byte func main() {b := make([]byte, 0)b = strconv.AppendQuoteRune(b, '好')fmt.Printf("%s", b) // '好' }QuoteRuneToASCII?
// QuoteRuneToASCII 將 Unicode 字符轉換為“單引號”引起來的 ASCII 字符串 // “非 ASCII 字符”和“特殊字符”將被轉換為“轉義字符” func QuoteRuneToASCII(r rune) string func main() {fmt.Println(strconv.QuoteRuneToASCII('好'))// '\u597d' }AppendQuoteRune?
// AppendQuoteRune 將 Unicode 字符轉換為“單引號”引起來的 ASCII 字符串, // 并將結果追加到 dst 的尾部,返回追加后的 []byte // “非 ASCII 字符”和“特殊字符”將被轉換為“轉義字符” func AppendQuoteRuneToASCII(dst []byte, r rune) []bytefunc main() {b := make([]byte, 0)b = strconv.AppendQuoteRuneToASCII(b, '好')fmt.Printf("%s", b) // '\u597d' }CanBackquote?
// CanBackquote 判斷字符串 s 是否可以表示為一個單行的“反引號”字符串 // 字符串中不能含有控制字符(除了 \t)和“反引號”字符,否則返回 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 中的第一個字符“取消轉義”并解碼 // // s:轉義后的字符串 // quote:字符串使用的“引號符”(用于對引號符“取消轉義”) // // value: 解碼后的字符 // multibyte:value 是否為多字節(jié)字符 // tail: 字符串 s 除去 value 后的剩余部分 // error: 返回 s 中是否存在語法錯誤 // // 參數(shù) quote 為“引號符” // 如果設置為單引號,則 s 中允許出現(xiàn) \' 字符,不允許出現(xiàn)單獨的 ' 字符 // 如果設置為雙引號,則 s 中允許出現(xiàn) \" 字符,不允許出現(xiàn)單獨的 " 字符 // 如果設置為 0,則不允許出現(xiàn) \' 或 \" 字符,可以出現(xiàn)單獨的 ' 或 " 字符 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 將“帶引號的字符串” s 轉換為常規(guī)的字符串(不帶引號和轉義字符) // s 可以是“單引號”、“雙引號”或“反引號”引起來的字符串(包括引號本身) // 如果 s 是單引號引起來的字符串,則返回該該字符串代表的字符 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 是否是一個可顯示的字符 // 可否顯示并不是你想象的那樣,比如空格可以顯示,而\t則不能顯示 // 具體可以參考 Go 語言的源碼 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 }?
總結
以上是生活随笔為你收集整理的Go的strconv二的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Go的strconv一
- 下一篇: 100kg等于多少吨?