This is a creation in Article, where the information may have evolved or changed.
This article goes from Golove Blog: http://www.cnblogs.com/golove/p/3262925.html
Functions and methods in the StrConv package
Atob.go
------------------------------------------------------------
Parsebool converting a string to a Boolean value
It accepts the truth: 1, T, T, True, true, true
It accepts false values: 0, F, F, false, false, false.
Any other value returns an error
Func parsebool (str string) (value bool, err error)
Package Mainimport ("FMT" "StrConv") 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 converts a Boolean value to the string "true" or "false"
Func Formatbool (b bool) string
Func Main () {FMT. Println (StrConv. Formatbool (0 < 1))//TRUEFMT. Println (StrConv. Formatbool (0 > 1))//False}
------------------------------------------------------------
Appendbool Converts the Boolean value B to the string "true" or "false"
The result is then appended to the end of DST, returning the appended []byte
Func Appendbool (DST []byte, b bool) []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)//TrueFalse}
============================================================
Atof.go
------------------------------------------------------------
Parsefloat converting a string to a floating-point number
S: the string to convert
Bitsize: Specifying floating-point types (32:float32, 64:float64)
If s is a valid format and is close to a floating-point value,
Returns the rounding value of the floating-point number (according to the rounding criteria of IEEE754)
Returns "Syntax error" if s is not a valid format
Returns "Out of range" if the result of the conversion is outside the Bitsize range
Func parsefloat (s string, bitsize int) (f float64, err Error)
Func Main () {s: = "0.12345678901234567890" F, err: = StrConv. Parsefloat (S, +) FMT. Println (f, Err) //0.12345679104328156FMT. Println (float32 (f), err)//0.12345679f, err = StrConv. Parsefloat (S, +) FMT. Println (f, Err)//0.12345678901234568}
============================================================
Atoi.go
------------------------------------------------------------
Errrange indicates that the value is out of range
var errrange = errors. New ("Value out of range")
Errsyntax indicates incorrect syntax
var errsyntax = errors. New ("Invalid Syntax")
Numerror Record conversion failed
Type Numerror struct {
Func string//Failed function name (Parsebool, parseint, Parseuint, parsefloat)
Num string//input value
ERR error//Cause of failure (Errrange, Errsyntax)
}
Length of type int or UINT (32 or 64)
Const INTSIZE = IntSize
Const INTSIZE = << UINT (^uint (0) >>63)
Implementing the error interface and outputting the wrong message
Func (e *numerror) Error () string
------------------------------------------------------------
Parseint Converting a string to an int type
S: the string to convert
Base: Carry System (2 binary to 36 binary)
Bitsize: Specifying integer types (0:int, 8:int8, 16:int16, 32:int32, 64:int64)
Returns the converted results and errors encountered during the conversion
If base is 0, the carry system is judged based on the prefix of the string (0x:16,0:8, others: 10)
Func parseint (s string, base int, bitsize int) (I int64, err error)
Func Main () {FMT. Println (StrConv. parseint ("123", ten, 8))//123FMT. Println (StrConv. parseint ("12345", ten, 8))//127 StrConv. Parseint:parsing "12345": Value out of rangefmt. Println (StrConv. parseint ("2147483647", ten, 0))//2147483647FMT. Println (StrConv. parseint ("0xFF", 0))//0 StrConv. Parseint:parsing "0xFF": Invalid SYNTAXFMT. Println (StrConv. parseint ("FF", 0)//255fmt. Println (StrConv. parseint ("0xFF", 0, 0)//255}
------------------------------------------------------------
The Parseuint function is the same as parseint, except that it returns a UINT type integer
Func Parseuint (s string, base int, bitsize int) (n UInt64, err Error)
Func Main () {FMT. Println (StrConv. Parseuint ("FF", 16, 8)//255}
------------------------------------------------------------
Atoi equivalent to parseint (S, 10, 0)
This function is usually used instead of using the parseint
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}
============================================================
Ftoa.go
------------------------------------------------------------
Formatfloat to convert a floating-point number F to a string value
F: Floating-point number to convert
FMT: Format tags (b, E, E, F, G, g)
PREC: Precision (length of the number part, excluding exponential portion)
Bitsize: Specifying floating-point types (32:float32, 64:float64)
//
Format Tags:
' B ' (-ddddp±ddd, binary index)
' E ' (-d.dddde±dd, decimal index)
' E ' (-d.dddde±dd, decimal index)
' F ' (-DDD.DDDD, no index)
' G ' (' e ': large exponent, ' F ': other case)
' G ' (' E ': large exponent, ' F ': other case)
//
If the format is marked ' E ', ' e ' and ' f ', then prec represents the number of digits after the decimal point
If the format is marked ' g ', ' g ', then prec represents the total number of digits (integral part + fractional part)
Func formatfloat (f float64, FMT Byte, prec, Bitsize int) string
Package Mainimport ("FMT" "StrConv") func main () {f: = 100.12345678901234567890123456789fmt. Println (StrConv. Formatfloat (f, ' B ', 5, +))//13123382P-17FMT. Println (StrConv. Formatfloat (F, ' E ', 5, +))//1.00123E+02FMT. Println (StrConv. Formatfloat (F, ' E ', 5, +))//1.00123E+02FMT. Println (StrConv. Formatfloat (F, ' F ', 5, +))//100.12346FMT. Println (StrConv. Formatfloat (F, ' G ', 5, +))//100.12FMT. Println (StrConv. Formatfloat (F, ' G ', 5, +))//100.12FMT. Println (StrConv. Formatfloat (f, ' B ', +, +))//13123382P-17FMT. Println (StrConv. Formatfloat (F, ' e ', +, +))//1.001234588623046875000000000000E+02FMT. Println (StrConv. Formatfloat (F, ' E ', +, +))//1.001234588623046875000000000000E+02FMT. Println (StrConv. Formatfloat (F, ' F ', +, +))//100.123458862304687500000000000000FMT. Println (StrConv. Formatfloat (F, ' G ', +, +))//100.1234588623046875FMT. Println (StrConv. Formatfloat (F, ' G ', 30, 32))//100.1234588623046875}
------------------------------------------------------------
Appendfloat converts a floating-point number F to a string value and appends the result of the conversion to the tail of DST
Returns the appended []byte
Func appendfloat (DST []byte, F float64, FMT byte, prec int, bitsize int) []byte
Func main () {f: = 100.12345678901234567890123456789b: = Make ([]byte, 0) b = StrConv. Appendfloat (b, F, ' F ', 5, +) b = append (b, "" ...) b = StrConv. Appendfloat (b, F, ' E ', 5, +) FMT. Printf ("%s", b)//100.12346 1.00123e+0}
============================================================
Itoa.go
------------------------------------------------------------
Formatuint converts int i to string form
Base: Carry System (2 binary to 36 binary)
Number greater than 10, return value using lowercase ' a ' to ' Z '
Func Formatint (i int64, base int) string
Func Main () {i: = Int64 ( -2048) fmt. Println (StrConv. Formatint (i, 2)) // -100000000000FMT. Println (StrConv. Formatint (I, 8)) // -4000FMT. Println (StrConv. Formatint (I, ten))// -2048FMT. Println (StrConv. Formatint (i, +))// -800FMT. Println (StrConv. Formatint (i, I))// -1kw}
------------------------------------------------------------
Formatuint Converting a uint integer i to a string form
Base: Carry System (2 binary to 36 binary)
Number greater than 10, return value using lowercase ' a ' to ' Z '
Func Formatuint (i uint64, base int) string
Func Main () {i: = UInt64 (2048) fmt. Println (StrConv. Formatuint (i, 2)) //100000000000FMT. Println (StrConv. Formatuint (I, 8)) //4000FMT. Println (StrConv. Formatuint (I, ten))//2048FMT. Println (StrConv. Formatuint (i, +))//800FMT. Println (StrConv. Formatuint (i, I))//1kw}
------------------------------------------------------------
Itoa equivalent to Formatint (i, 10)
Func Itoa (i int) string
Func Main () {FMT. Println (StrConv. Itoa ( -2048))// -2048FMT. Println (StrConv. Itoa (2048)) //2048}
------------------------------------------------------------
Appendint converts int i to string form and appends to the end of DST
I: the string to convert
Base: Binary
Returns the appended []byte
Func appendint (DST []byte, I int64, base int) []byte
Func main () {b: = make ([]byte, 0) b = StrConv. Appendint (b, -2048, +) fmt. Printf ("%s", b)//-800}
------------------------------------------------------------
Appenduint converts a uint integer i to a string form and appends to the end of the DST
I: the string to convert
Base: Binary
Returns the appended []byte
Func appenduint (DST []byte, I uint64, base int) []byte
Func Main () {
B: = make ([]byte, 0)
b = StrConv. Appenduint (b, 2048, 16)
Fmt. Printf ("%s", b)//800
}
============================================================
Quote.go
------------------------------------------------------------
Quote string s converted to double quotation marks
The special characters will be converted to "escape characters"
"Non-displayed characters" will be converted to "escape character"
Func Quote (s string) string
Func Main () {FMT. Println (StrConv. Quote (' C:\Windows '))//"C:\\Windows"}
Note: Here is the back quotation mark (the button on the left of 1 on the keyboard), not the single quotation mark
------------------------------------------------------------
Appendquote converts the string s to a string caused by "double quotation marks",
and appends the result to the end of DST, returning the appended []byte
The special characters will be converted to "escape characters"
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 ASCII String that converts the string s to "double quotes"
"Non-ASCII characters" and "special characters" will be converted to "escape characters"
Func Quotetoascii (s string) string
Func Main () {FMT. Println (StrConv. Quotetoascii ("Hello world!") "))//" Hello \u4e16\u754c\uff01 "}
------------------------------------------------------------
Appendquotetoascii converts the string s to the ASCII string that is caused by the "double quotation mark".
and appends the result to the end of DST, returning the appended []byte
"Non-ASCII characters" and "special characters" will be converted to "escape characters"
Func appendquotetoascii (DST []byte, S string) []byte
Func Main () {s: = "Hello world!" "B: = make ([]byte, 0) b = StrConv. Appendquotetoascii (b, s) fmt. Printf ("%s", B)//"Hello \u4e16\u754c\uff01"}
------------------------------------------------------------
Quoterune A string that converts Unicode characters to single quotation marks
"Special character" will be converted to "escape character"
Func Quoterune (R rune) string
Func Main () {
Fmt. Println (StrConv. Quoterune (' good '))
Good
}
Note: This is a single quote, not an anti-quote, which should be separated from quote ().
------------------------------------------------------------
Appendquoterune converts a Unicode character to a string that is caused by "single quotation marks".
and appends the result to the end of DST, returning the appended []byte
"Special character" will be converted to "escape character"
Func Appendquoterune (DST []byte, R rune) []byte
Func main () {b: = make ([]byte, 0) b = StrConv. Appendquoterune (b, ' good ') fmt. Printf ("%s", b)//' good '}
------------------------------------------------------------
Quoterunetoascii ASCII String that converts Unicode characters to "single quotes"
"Non-ASCII characters" and "special characters" will be converted to "escape characters"
Func quoterunetoascii (R rune) string
Func Main () {FMT. Println (StrConv. Quoterunetoascii (' good '))//' \u597d '}
------------------------------------------------------------
Appendquoterune converts a Unicode character to an ASCII string that is caused by the "single quotation mark".
and appends the result to the end of DST, returning the appended []byte
"Non-ASCII characters" and "special characters" will be converted to "escape characters"
Func appendquoterunetoascii (DST []byte, R rune) []byte
Func main () {b: = make ([]byte, 0) b = StrConv. Appendquoterunetoascii (b, ' good ') fmt. Printf ("%s", b)//' \u597d '}
------------------------------------------------------------
Canbackquote determines whether the string s can be represented as a single line of "anti-quote" strings
The string cannot contain a control character (except \ t) and an "anti-quote" character, otherwise it returns 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 "de-escapes" the first character in S and decodes
//
S: escaped string
Quote: The "quotation mark" used by the string (used to "de-escape" the quote symbol)
//
Value: the decoded character
Whether the Multibyte:value is a multibyte character
Tail: string S is removed after the remainder of value
Error: Returns the presence of a syntax error in s
//
The parameter quote is "quotation mark character"
If set to single quotation marks, the \ ' character is allowed in S, and a separate ' character is not allowed to appear
If set to double quotation marks, the \ "character is allowed in S, and a separate" character is not allowed to appear
If set to 0, the \ ' or \ ' character is not allowed, and a separate ' or ' character can appear
Func Unquotechar (s string, quote byte) (value rune, multibyte bool, tail string, err Error)
Func Main () {s: = ' \ ' big \ \ Good! \ "' 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//big true//\ false//Home true//\ false//Good true//!" True
------------------------------------------------------------
Unquote converts "quoted string" s to regular string (without quotation marks and escape characters)
S can be "single quote", "double quote", or "anti-quote" string (including the quotation marks themselves)
If S is a string enclosed in single quotation marks, the character represented by the string is returned
Func unquote (S string) (t string, err Error)
Func Main () {sr, err: = StrConv. Unquote (' "\" big \ t home \ t! \ "') fmt. Println (SR, err) SR, err = StrConv. Unquote (' Hello everybody! ") Fmt. Println (SR, err) SR, err = StrConv. Unquote (' good ') fmt. Println (SR, err) SR, err = StrConv. Unquote ("' Big \\t home \\t good! ' ") fmt. Println (SR, err)}
------------------------------------------------------------
Isprint to determine if the Unicode character R is a displayable character
Can the display not be what you think, such as a space to display, and \ t to display
Refer to the source code of the Go language specifically
Func isprint (R rune) bool
Func Main () {FMT. Println (StrConv. Isprint (' a ')) //TRUEFMT. Println (StrConv. Isprint (' good ')) //TRUEFMT. Println (StrConv. Isprint (")) //TRUEFMT. Println (StrConv. Isprint (' \ t '))//FALSEFMT. Println (StrConv. Isprint (' \ n '))//FALSEFMT. Println (StrConv. Isprint (0)) //False}