This is a creation in Article, where the information may have evolved or changed.
Practice Golang String Common operations, backing up
Type conversions
Compare and contrast
Search Find Statistics
Replace
Delete filter
Uppercase and lowercase conversions
Prefix suffix
String segmentation
Stitching
3 Stitching strings in comparison
Func Main () {FMT. Println ("string test") fmt. Println ("string Conversion")//Gets the number of bits of the int type under the operating system platform under which the program runs, such as: StrConv. IntSize. StrConv. IntSize FMT. Println ("Converts the string to int. ") var Trastr01 string =" traint01 ", Err_tra: = StrConv. Atoi (TRASTR01) if Err_tra! = nil {fmt. Println (Err_tra)} else {fmt. Println (traint01)} fmt. Println ("Convert String to Float64 type") var trastr02 string = "100.55" trafloat01, err_float: = StrConv. Parsefloat (Trastr02, ten) if err_float! = nil {fmt. Println (Err_float)} else {fmt. Println (trafloat01)} trastr03: = StrConv. Itoa (a) fmt. PRINTLN ("int to Word characters" + trastr03) var Str01 string = "Hello,world" Str02: = "Hello, world" FMT. Println (Str01) fmt. PRINTLN (STR02)/////string comparison com01: = Strings.compare (Str01, str02) if com01 = = 0 {fmt. Println ("equal")} else {fmt. Println ("unequal" + string (COM01))} FMT. PRINTLN (COM01)//find contains var iscon bool = strings. Contains (Str01, "Hello") FMt. Println (Iscon)//true//Find location var theindex int = strings. Index (Str01, ",") fmt. Println (theindex)//5 FMT. PRINTLN (Strings. Index (Str01, "haha"))//does not exist return-1 LastIndex: = Strings. LastIndex (Str01, "O") fmt. Println ("Index of last occurrence in string" + StrConv. Itoa (LastIndex))//7//-1 indicates that the string s does not contain a string//Count the occurrences of Sep for a given substring, and when Sep is null, returns 1 + the length of the string fmt. PRINTLN (Strings. Count ("Cheeseeee", "ee"))//3 FMT. PRINTLN (Strings. Count ("Five", ""))//5//repeats the S string Count times, and finally returns the newly generated duplicate string fmt. Println ("Hello" + strings. Repeat ("World")) FMT. PRINTLN ("replace")///In the S string, replace the old string with the new string, n indicates the number of substitutions, and less than 0 means replace all var str03 string = "/users//documents/gopatch/src/mygo/ config/teststring/"str04: = Strings. Replace (STR03, "/", "* *",-1) str05: = Strings. Replace (STR03, "/", "* *", 4) fmt. Println (str04)//**users****documents**gopatch**src**mygo**config**teststring** FMT. Println (str05)//**users****documents**gopatch/src/mygo/config/teststring/fmt. Println ("Delete stringOpening and trailing ") FMT. Println ("Delete both ends/=" + strings.) Trim (STR03, "/"))//users//documents/gopatch/src/mygo/config/teststring FMT. Println ("Delete left/=" + strings. Trimleft (STR03, "/"))//users//documents/gopatch/src/mygo/config/teststring///And trimright str06: = Strings. Trimspace ("Hello Hao Hao Hao") fmt. Println ("Remove the space at the beginning of the end =" + str06)//' Hello Hao Hao Hao ' fmt. PRINTLN ("Case") str07: = "Hello Hao hao Hao" fmt. PRINTLN (Strings. Title (str07))//hello Hao Hao Hao fmt. PRINTLN (Strings. ToLower ("Hello Hao Hao Hao"))//Hello Hao Hao Hao fmt. PRINTLN (Strings. ToUpper (str07))//hello HAO HAO HAO//prefix suffix FMT. PRINTLN (Strings. Hasprefix ("Gopher", "Go")//True FMT. PRINTLN (Strings. Hassuffix ("Amigo", "Go")//True FMT. Println ("string Split") Fieldsstr: = "Hello it's a nice Day Today"//split by whitespace character, without qualifying a few whitespace characters in the middle interval fieldsslece: = Strin Gs. Fields (FIELDSSTR) fmt. Println (fieldsslece)//[hello it ' s a nice Day today] for I, V: = RanGE fieldsslece {FMT. Printf ("subscript%d corresponding value =%s \ n", I, v)} for I: = 0; I < Len (fieldsslece); i++ {fmt. Println (Fieldsslece[i])}//Split slice01 according to specific characters: = strings. Split ("Q,w,e,r,t,y,", ",") fmt. Println (SLICE01)//[q W e r t y] fmt. Println (Cap (SLICE01))//7 the last number of empty "" For I, V: = Range slice01 {fmt. Printf ("subscript%d corresponding value =%s \ n", I, V)}//Stitching//join is used to slice the element type string, using a split symbol to stitch together a string: var str08 string = Strings. Join (Fieldsslece, ",") fmt. PRINTLN ("Join stitching result =" + str08)//hello,it ' S,a,nice,day,today fmt. Println ("------------contrast string concatenation efficiency----------------") var buffer bytes. Buffer Start: = time. Now () for I: = 0; I < 100000; i++ {buffer. WriteString ("Test is here\n")} buffer. String ()//stitching result End: = time. Now () Fmt. Println ("Buffer time is", end.) Sub (Start). Seconds ()) Start = time. Now () str: = "" For I: = 0; I < 100000; i++ {str + = "Test is here\n"} end = time. Now () Fmt. PrIntln ("+ = Time is", end.) Sub (Start). Seconds ()) Start = time. Now () var sl []string for I: = 0; I < 100000; i++ {SL = append (SL, "Test is here\n")} strings. Join (SL, "") End = time. Now () Fmt. Println ("Join time is", end.) Sub (Start). Seconds ())/* Buffer time is 0.00388283 + = time is 11.730007558 Join time is 0.016644653 */}