1 Package Main2 3 Import (4 "FMT"5 "StrConv"6 "Strings"7 )8 9 Func Main () {Ten//Strings Demo One Contains () A Join () - Index () - Repeat () the Replace () - Split () - -//StrConv Demo + Appenddemo () - Formatdemo () + Parsedemo () A } at - func Contains () { -Fmt. PRINTLN (Strings. Contains ("Seafood","Foo")) //true -Fmt. PRINTLN (Strings. Contains ("Seafood","Bar")) //false - } - in func Join () { -s: = []string{"Foo","Bar","Baz"} toFmt. PRINTLN (Strings. Join (S,",")) //Foo,bar,baz + } - the func Index () { *Fmt. PRINTLN (Strings. Index ("Chicken","Ken"))//4 $Fmt. PRINTLN (Strings. Index ("Chicken","DMR"))//-1Panax Notoginseng } - the func Repeat () { +Fmt. Println ("BA"+ Strings. Repeat ("na", 2))//Banana A } the + func Replace () { -Fmt. PRINTLN (Strings. Replace ("Oink Oink Oink","k","KY", 2))//oinky oinky Oink $Fmt. PRINTLN (Strings. Replace ("Oink Oink Oink","Oink","Moo",-1))//Moo Moo Moo $ } - - func Split () { theFmt. Printf ("%q\n", strings. Split ("A,b,c",",")) //["a" "b" "C"] -Fmt. Printf ("%q\n", strings. Split ("A man a plan a canal Panama","a")) //["" "Mans" "Plan" "Canal Panama"]WuyiFmt. Printf ("%q\n", strings. Split ("XYZ","")) //[" " "x" "y" "Z" " "] theFmt. Printf ("%q\n", strings. Split ("","Bernardo O ' Higgins")) //[""] - } Wu - func Appenddemo () { Aboutstr: = make ([]byte, 0, 100) $str = StrConv. Appendint (str, 4567, 10) -str =StrConv. Appendbool (str, FALSE) -str = StrConv. Appendquote (str,"ABCDEFG") -str = StrConv. Appendquoterune (str,'single') AFmt. PRINTLN (String (str))//4567false"ABCDEFG"'single' + } the - func Formatdemo () { $A: =StrConv. Formatbool (False) theB: = StrConv. Formatfloat (123.23,'g', 12, 64) thec: = StrConv. Formatint (1234, 10) theD: = StrConv. Formatuint (12345, 10) theE: = StrConv. Itoa (1023) -Fmt. Println (A, B, C, D, E)//False 123.23 1234 12345 1023 in } the the func Parsedemo () { AboutA, err: = StrConv. Parsebool ("false") the Handleerr (Err) theB, err: = StrConv. Parsefloat ("123,23", 64) the Handleerr (Err) +C, err: = StrConv. parseint ("1234", 10, 64) - Handleerr (Err) theD, err: = StrConv. Parseuint ("12345", 10, 64)Bayi Handleerr (Err) theE: = StrConv. Itoa (1023) the Handleerr (Err) -Fmt. Println (A, B, C, D, E)//False 0 1234 12345 1023 - } the the func Handleerr (err error) { the ifErr! =Nil { the FMT. PRINTLN (ERR) - } the}
Notes Golang String