Golang Study-StrConv Bag

Source: Internet
Author: User
Tags parse string printable characters
This is a creation in Article, where the information may have evolved or changed.
Converts a Boolean value to a string true or Falsefunc Formatbool (b bool) string//converts a string to a Boolean value//it accepts true value: 1, T, T, Real, true, true//it accepts false values: 0, F, F, f Alse, False, false//any other value returns an error. Func parsebool (str string) (bool, error)------------------------------//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")//Converts an integer to a string form. Base represents the conversion, with a value between 2 and 36. A number greater than 10 in the result is represented by a A-Z in lowercase letters. Func Formatint (i int64, base int) Stringfunc Formatuint (i uint64, base int) string//parse string to integer, parseint support sign, parseuint unsupported Sign. Base represents the Carry system (2 to 36), if base is 0, the string prefix is judged,//prefix 0x means 16, prefix 0 is 8, otherwise 10 is the binary. The bitsize represents the bit width of the result (including the sign bit), and 0 indicates the maximum bit width. Func parseint (s string, base int, bitsize int) (I int64, err error) Func Parseuint (s string, base int, bitsize int) (UInt64 , error)//Converts an integer to a decimal string (i.e., shorthand for formatint (I, 10)) func Itoa (i int) string//converts a string to a decimal integer, namely: parseint (S, 10, 0) func Atoi (s string) (int, error)------------------------------//example Func main () {FMT. PrintlN (StrConv. parseint ("FF", 0)//255fmt. Println (StrConv. parseint ("0xFF", 0))//0 StrConv. Parseint:parsing "0xFF": Invalid SYNTAXFMT. Println (StrConv. parseint ("0xFF", 0, 0)//255fmt. Println (StrConv. parseint ("9", ten, 4))//7 StrConv. Parseint:parsing "9": Value out of range}------------------------------//formatfloat to convert floating-point number f to string form/F: floating-point number to convert//FMT: Lattice (b, E, E, F, g, g)//PREC: Precision (length of the digital part, excluding exponential portion)//bitsize: Specifies the floating-point type (32:float32, 64:float64), and the result is rounded accordingly. Format Tags://' B ' (-ddddp±ddd, binary exponent)//' E ' (-d.dddde±dd, decimal index)//' E ' (-d.dddde±dd, decimal index)//' F ' (-ddd.dddd, no exponent)//' G ' (' E ' : Large exponent, ' F ': Other conditions)//' 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 (integer part + fractional part)//reference format input and output in the flag and precision description func formatfloat (f float64, FMT Byte, prec, Bitsize int) string//parses a string into a floating-point number, using I The EEE754 specification is rounded. The bigsize values are 32 and 642, indicating the precision of the conversion result. If there is a syntax error, err. Error = errsyntax//returns ±INF,ERR If the result is out of range. Error = Errrangefunc parsefloat (s string, bitsize int) (float64, error)------------------------------//Example 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}------------------------------//Determines whether the string can be modified to represent a single line of anti-quote strings. The string cannot contain a control character (except \ t) and an "anti-quote" character, otherwise return Falsefunc Canbackquote (s string) bool//example: Find all! The Canbackquote character Func main () {for I: = Rune (0); i < UTF8. Maxrune; i++ {if!strconv. Canbackquote (String (i)) {fmt.  Printf ("%q,", i)}}}//results as follows://' \x00 ', ' \x01 ', ' \x02 ', ' \x03 ', ' \x04 ', ' \x05 ', ' \x06 ', ' \ n ', ' \a ', ' \b ', ' \ R ', ' \x0e ', ' \x0f ', ' \x10 ', ' \x11 ', ' \x12 ', ' \x13 ', ' \x14 ', ' \x15 ', ' \x16 ', ' \x17 ', ' \x18 ', ' \x19 ', ' \x1a ', ' \x1b ', ' \x1c ', ' \x1d ', ' \x1e ', ' \x1f ', ' ', ' \u007f ', ' \ufeff '------------------------------//Determine if R is a printable character//can print is not what you think, such as spaces can be printed, and \ t cannot print the Func isprint (R rune) bool//determines whether R is a Unicode-defined graphic character. Func isgraphic (R rune) BOOL------------------------------//Example: Get non-printable characters and non-graphic characters func main () {var rnp, rng, Rpng, RGNP []runeconst maxlen = 32for I : = Rune (0); I < UTF8. Maxrune; i++ {if!strconv. Isprint (i) {//non-printable If Len (RNP) < MaxLen {RNP = append (RNP, i)}if StrConv. Isgraphic (i) && len (RGNP) < MaxLen {//graphics characters RGNP = append (RGNP, i)}}if!strconv. Isgraphic (i) {//non-graphic characters if Len (RNG) < MaxLen {rng = append (rng, i)}if StrConv. Isprint (i) && len (rpng) < MaxLen {//printable Rpng = append (Rpng, i)}}}fmt. Printf ("non-printable characters:%q\n", RNP) fmt. Printf ("Non-graphic character:%q\n", rng) fmt. Printf ("non-printable graphic characters:%q\n", RGNP) fmt. Printf ("Printable non-graphic character:%q\n", rpng)}//non-printable characters: [' \x00 ' \x01 ' ' \x02 ' "\x03 ' \x04 ' \x05 ' \x06 ' \a ', '" \ r \ n ' "\b" \v " "\ r" ' \x0e ' ' \x0f ' \x10 ' \x11 ' \x12 ' \x13 ', ' and ', \x14 ', ' \x15 ' ' \x16 ', ' and ' ' \x17 ', ' \x18 ', ' and ', ', ' the ', ' and ', ' \x19 ' \x1a ' \x1b ' \x1c 1e ' \x1f ']//non-graphic characters: [' \x00 ' \x01 ' \x02 ' ' \x03 ', ' \x04 ' and ' ' \x05 ' \x06 ' \a ' ' \ R ' \b ' \ t ' \ n ' ' \v ' ' \f ' \x0e F ' \x10 ' \x11 ' \x12 ' \x13 '' \x14 ' ' \x15 ' \x16 ' \x17 ' \x18 ' \x19 ', ' \x1a ', ' \x1b ', ' \x1c ', \x1d ' \x1e ' \x1f ']//' \u00a0 ' the non-printable graphic character: [' \u1680 ' ' \u2000 ' "\u2001" \u2002 "\u2003" \u2005 "\u2006" \u2007 "\u2008" ' \u2009 ' \u200a ' "\u202f", "\u2004", "the" "the", "and the" "The" "\u205f" printable non-graphic characters: []------------------------------//convert s to double quote string func Quote (s string) string//function, non-ASCII characters and nonprinting characters are escaped func Quotetoascii (s string) string//function, non-graphic character is escaped func quotetographic (s string) string------------------------------// Example Func Main () {s: = "hello\t world!" \ n "FMT. PRINTLN (s)//Hello World! (newline) fmt. Println (StrConv. Quote (s))//"Hello\t world! \ n "FMT. Println (StrConv. Quotetoascii (s))//"hello\t\u4e16\u754c\uff01\n" FMT. Println (StrConv. Quotetographic (s))//"Hello\t world! \ n "}------------------------------//convert R to single quote character Func Quoterune (R rune) string//function, non-ASCII characters and nonprinting characters are escaped func Quoterunetoascii (R rune) string//function, non-graphic characters are escaped by Func Quoterunetographic (R rune) string------------------------------ Unquote converts "quoted string" s toA regular string (without quotation marks and escape characters)//s can be a string (including the quotation marks itself) that is caused by the "single quote", "double quotation mark", or "anti-quotation mark"//If S is a string that is enclosed in single quotation marks, then returns the character that the string represents, Func unquote (S string) ( string, error)//Unquotechar "de-escaping" the first character in the quoted string (without the end-to-end quotation marks) and decoding////s: quoted string (does not contain the end-to-end quotation marks)//quote: String used by the "quote symbol" (Used to "de-escape" the quotation marks in a string)////value: Whether the decoded character//Multibyte:value is a multibyte character//tail: The remainder of the string s decoded//error: Returns the presence of a syntax error in s  The parameter quote is "quotation mark"///if set to single quotation marks, the S is allowed to appear \ ', ' character, do not allow separate ' characters//if set to double quotation marks, s in the Allow \ ", ' character, not allowed to appear separate" character//If set to 0, it is not allowed to appear \ ' or \ ' character, but can appear separate ' or ' character func Unquotechar (s string, quote byte) (value rune, multibyte bool, tail string, err error)-- ----------------------------//Example Func main () {s1: = "' Hello World! '//Parse back quote string s2: = ' "hello\t\u4e16\u754c\uff01" '//Parse double quote string FMT. Println (StrConv. Unquote (S1))//Hello World! <nil>fmt. Println (StrConv. unquote (S2))//Hello World! <nil>fmt. Println () fmt. Println (StrConv. Unquotechar (' \u4e16\u754c\uff01 ', 0))//19990 true \u754c\uff01 <nil>fmt. Println (StrConv. Unquotechar (' \ ' abc\ "', '" ')//False abc\ "&LT;NIL&GT;}------------------------------//convert various types to strings and append to the DST trailer. Func appendint (DST []byte, I int64, base int) []bytefunc appenduint (DST []byte, I uint64, base int) []bytefunc appendfloat (DST []byte, F float64, FMT Byte, prec, Bitsize int) []bytefunc Appendbool (DST []byte, b bool) []byte//the various types are converted to quoted strings appended to the DST trailer. Func appendquote (DST []byte, S string) []bytefunc appendquotetoascii (DST []byte, S string) []bytefunc Appendquotetographi C (DST []byte, S string) []bytefunc appendquoterune (DST []byte, R rune) []bytefunc appendquoterunetoascii (DST []byte, R run e) []bytefunc appendquoterunetographic (DST []byte, R rune) [] BYTE------------------------------------------------------------

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.