2package Main Import ( "FMT" "Strings" "Unicode/utf8" ) Func Main () { Fmt. Println ("Find the substring in the specified string") Fmt. Println ("Use of Contains function") Fmt. PRINTLN (Strings. Contains ("Seafood", "foo"))//true Fmt. PRINTLN (Strings. Contains ("Seafood", "bar")//false Fmt. PRINTLN (Strings. Contains ("Seafood", ""))//true Fmt. PRINTLN (Strings. Contains ("", "")//true here to pay special attention to Fmt. PRINTLN (Strings. Contains ("I am Chinese", "I")//true Fmt. Println ("") Fmt. Println ("Use of Containsany function") Fmt. PRINTLN (Strings. Containsany ("Team", "I"))//False Fmt. PRINTLN (Strings. Containsany ("Failure", "U & I"))//True Fmt. PRINTLN (Strings. Containsany ("foo", ""))//False Fmt. PRINTLN (Strings. Containsany ("", ""))//False Fmt. Println ("") Fmt. Println ("Use of Containsrune function") Fmt. PRINTLN (Strings. Containsrune ("I am China", ' I '))//True note the second argument, with a character Fmt. Println ("") Fmt. Println ("Use of the Count function") Fmt. PRINTLN (Strings. Count ("Cheese", "e"))//3 Fmt. PRINTLN (Strings. Count ("Five", ""))//Before & after each rune Result:5, the source code has implemented Fmt. Println ("") Fmt. Println ("Use of equalfold function") Fmt. PRINTLN (Strings. Equalfold (' Go ', ' go ')//Case ignored Fmt. Println ("") Fmt. Println ("Use of Fields function") Fmt. Println ("Fields are:%q", strings. Fields ("foo bar baz"))//["foo" "Bar" "Baz"] returns a list The equivalent of using a function as a parameter to support anonymous functions For _, Record: = range []string{"aaa*1892*122", "Aaataat", "124|939|22"} { Fmt. PRINTLN (Strings. Fieldsfunc (Record, func (Ch rune) bool { Switch { Case CH > ' 5 ': return True } return False })) } Fmt. Println ("") Fmt. Println ("Use of Hasprefix function") Fmt. PRINTLN (Strings. Hasprefix ("Nlt_abc", "NLT"))//prefix is started with NLT Fmt. Println ("") Fmt. Println ("Use of Hassuffix function") Fmt. PRINTLN (Strings. Hassuffix ("Nlt_abc", "abc"))//suffix begins with NLT Fmt. Println ("") Fmt. Println ("Use of Index function") Fmt. PRINTLN (Strings. Index ("Nlt_abc", "abc")//returns the position of the first matching character, here is 4 Fmt. PRINTLN (Strings. Index ("Nlt_abc", "AAA"))//return in existence-1 Fmt. PRINTLN (Strings. Index ("I am Chinese", "Medium"))//in existence return 6 Fmt. Println ("") Fmt. Println ("Use of Indexany function") Fmt. PRINTLN (Strings. Indexany ("I am Chinese", "Medium"))//in existence return 6 Fmt. PRINTLN (Strings. Indexany ("I am Chinese", "and")//In existence return-1 Fmt. Println ("") Fmt. Println ("Use of Index function") Fmt. PRINTLN (Strings. Indexrune ("Nlt_abc", ' B '))//Returns the position of the first matching character, here is 4 Fmt. PRINTLN (Strings. Indexrune ("Nlt_abc", ' s '))//In Presence return-1 Fmt. PRINTLN (Strings. Indexrune ("I am Chinese", ' in '))//in existence return 6 Fmt. Println ("") Fmt. Println ("Use of Join function") s: = []string{"foo", "Bar", "Baz"} Fmt. PRINTLN (Strings. Join (S, ","))//return string: Foo, bar, Baz Fmt. Println ("") Fmt. Println ("Use of lastindex function") Fmt. PRINTLN (Strings. Lastindex ("Go Gopher", "Go"))//3 Fmt. Println ("") Fmt. Println ("Use of Lastindexany function") Fmt. PRINTLN (Strings. Lastindexany ("Go Gopher", "Go"))//4 Fmt. PRINTLN (Strings. Lastindexany ("I am Chinese", "Zhong"))//6 Fmt. Println ("") Fmt. Println ("Use of Map function") ROT13: = func (R rune) Rune { Switch { Case r >= ' A ' && r <= ' Z ': Return ' a ' + (R ' a ' +13)%26 Case R >= ' a ' && r <= ' z ': Return ' a ' + (R ' a ' +13)%26 } Return r } Fmt. PRINTLN (Strings. Map (rot13, "' Twas Brillig and the slithy gopher ...")) Fmt. Println ("") Fmt. Println ("Use of Repeat function") Fmt. Println ("ba" + Strings. Repeat ("NA", 2)//banana Fmt. Println ("") Fmt. Println ("Use of Replace function") Fmt. PRINTLN (Strings. Replace ("Oink oink Oink", "K", "KY", 2)) Fmt. PRINTLN (Strings. Replace ("Oink oink oink", "Oink", "moo",-1)) Fmt. Println ("") Fmt. Println ("Use of Split function") Fmt. Printf ("%qn", strings. Split ("A,b,c", ",")) Fmt. Printf ("%qn", strings. Split ("A man a plan a canal Panama", "a")) Fmt. Printf ("%qn", strings. Split ("xyz", "")) Fmt. Printf ("%qn", strings. Split ("", "Bernardo O ' Higgins")) Fmt. Println ("") Fmt. Println ("Use of splitafter function") Fmt. Printf ("%qn", strings. Splitafter ("/home/m_ta/src", "/"))//["/" "home/" "m_ta/" "src"] Fmt. Println ("") Fmt. Println ("Use of Splitaftern function") Fmt. Printf ("%qn", strings. Splitaftern ("/home/m_ta/src", "/", 2))//["/" "HOME/M_TA/SRC"] Fmt. Printf ("%qn", strings. Splitaftern ("#home #m_ta#src", "#",-1))//["/" "home/" "m_ta/" "src"] Fmt. Println ("") Fmt. Println ("Use of splitn function") Fmt. Printf ("%qn", strings. SPLITN ("/home/m_ta/src", "/", 1)) Fmt. Printf ("%qn", strings. SPLITN ("/home/m_ta/src", "/", 2))//["/" "home/" "m_ta/" "src"] Fmt. Printf ("%qn", strings. SPLITN ("/home/m_ta/src", "/",-1))//["" "Home" "M_ta" "src"] Fmt. Printf ("%qn", strings. SPLITN ("Home,m_ta,src", ",", 2))//["/" "home/" "m_ta/" "src"] Fmt. Printf ("%qn", strings. SPLITN ("#home #m_ta#src", "#",-1))//["/" "home/" "m_ta/" "src"] Fmt. Println ("") Fmt. Println ("Use of Title function")//This function, I really don't know what to use Fmt. PRINTLN (Strings. Title ("Her Royal Highness")) Fmt. Println ("") Fmt. Println ("Use of ToLower function") Fmt. PRINTLN (Strings. ToLower ("Gopher"))//gopher Fmt. Println ("") Fmt. Println ("Use of tolowerspecial function") Fmt. Println ("") Fmt. Println ("Use of totitle function") Fmt. PRINTLN (Strings. Totitle ("Loud noises")) Fmt. PRINTLN (Strings. Totitle ("Loud China")) Fmt. Println ("") Fmt. Println ("Use of Replace function") Fmt. PRINTLN (Strings. Replace ("Abaacedf", "a", "a", 2))//ABAACEDF The fourth argument is less than 0, which means that all are replaced, and you can look at the Golang document Fmt. PRINTLN (Strings. Replace ("Abaacedf", "a", "a",-1))//ABAACEDF Fmt. Println ("") Fmt. Println ("Use of ToUpper function") Fmt. PRINTLN (Strings. ToUpper ("Gopher"))//gopher Fmt. Println ("") Fmt. Println ("Use of Trim function") Fmt. Printf ("[%q]", strings. Trim ("!!! Achtung!!! ", "! ")) ["Achtung"] Fmt. Println ("") Fmt. Println ("Use of trimleft function") Fmt. Printf ("[%q]", strings. Trimleft ("!!! Achtung!!! ", "! ")) ["Achtung!!!"] Fmt. Println ("") Fmt. Println ("Use of trimspace function") Fmt. PRINTLN (Strings. Trimspace ("tn a lone Gopher ntrn"))//a lone Gopher } |