2 package main Import ( "Fmt" "Strings" // "Unicode/utf8" ) Func main (){ Fmt. Println ("checking whether the substring is in the specified string ") Fmt. Println ("Contains function usage ") 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 special attention here Fmt. Println (strings. Contains ("I am Chinese", "I") // true Fmt. Println ("") Fmt. Println ("ContainsAny function usage ") 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 ("ContainsRune function usage ") Fmt. Println (strings. ContainsRune ("I am China", 'I') // true pay attention to the second parameter, which is a character Fmt. Println ("") Fmt. Println ("Count function usage ") Fmt. Println (strings. Count ("cheese", "e") // 3 Fmt. Println (strings. Count ("five", "") // before & after each rune result: 5, implemented in source code Fmt. Println ("") Fmt. Println ("usage of implicit fold function ") Fmt. Println (strings. Character fold ("Go", "go") // case insensitive Fmt. Println ("") Fmt. Println ("Fields function usage ") Fmt. Println ("Fields are: % q", strings. Fields ("foo bar baz") // ["foo" "bar" "baz"] returns a list // Equivalent to using a function as a parameter and supporting 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 ("HasPrefix function usage ") Fmt. Println (strings. HasPrefix ("NLT_abc", "NLT") // The prefix starts with NLT Fmt. Println ("") Fmt. Println ("HasSuffix function usage ") Fmt. Println (strings. HasSuffix ("NLT_abc", "abc") // The suffix starts with NLT. Fmt. Println ("") Fmt. Println ("Index function usage ") Fmt. Println (strings. Index ("NLT_abc", "abc") // returns the position of the first matched character. Here is 4 Fmt. Println (strings. Index ("NLT_abc", "aaa") // If yes-1 is returned Fmt. Println (strings. Index ("I am a Chinese", "medium") // if there is an error, 6 is returned. Fmt. Println ("") Fmt. Println ("IndexAny function usage ") Fmt. Println (strings. IndexAny ("I am a Chinese", "medium") // if there is a returned 6 Fmt. Println (strings. IndexAny ("I am a Chinese", "and") // returns-1 in the Presence Fmt. Println ("") Fmt. Println ("Index function usage ") Fmt. Println (strings. IndexRune ("NLT_abc", 'B') // returns the position of the first matched character. Here is 4 Fmt. Println (strings. IndexRune ("NLT_abc", 's') // if an existing one returns-1 Fmt. Println (strings. IndexRune ("I am a Chinese", 'zhong') // if the object exists, 6 is returned. Fmt. Println ("") Fmt. Println ("Join function usage ") S: = [] string {"foo", "bar", "baz "} Fmt. Println (strings. Join (s, ",") // return string: foo, bar, baz Fmt. Println ("") Fmt. Println ("LastIndex function usage ") Fmt. Println (strings. LastIndex ("go gopher", "go") // 3 Fmt. Println ("") Fmt. Println ("LastIndexAny function usage ") Fmt. Println (strings. LastIndexAny ("go gopher", "go") // 4 Fmt. Println (strings. LastIndexAny ("I am a Chinese", "medium") // 6 Fmt. Println ("") Fmt. Println ("Map function usage ") 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 ("Repeat function usage ") Fmt. Println ("ba" + strings. Repeat ("na", 2) // banana Fmt. Println ("") Fmt. Println ("Replace function usage ") Fmt. Println (strings. Replace ("oink", "k", "ky", 2 )) Fmt. Println (strings. Replace ("oink", "oink", "moo",-1 )) Fmt. Println ("") Fmt. Println ("usage of the Split function ") Fmt. Printf ("% qn", strings. Split ("a, B, c ",",")) Fmt. Printf ("% qn", strings. Split ("a man a plan a canal panama", "")) Fmt. Printf ("% qn", strings. Split ("xyz ","")) Fmt. Printf ("% qn", strings. Split ("", "Bernardo O 'Higgins ")) Fmt. Println ("") Fmt. Println ("SplitAfter function usage ") Fmt. printf ("% qn", strings. splitAfter ("/home/m_ta/src", "/") // ["/" home/"m_ta/" "src"] Fmt. Println ("") Fmt. Println ("SplitAfterN function usage ") 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 ("SplitN function usage ") 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 ("Title function usage") // This function does not know how to use it. Fmt. Println (strings. Title ("her royal highness ")) Fmt. Println ("") Fmt. Println ("ToLower function usage ") Fmt. Println (strings. ToLower ("Gopher") // gopher Fmt. Println ("") Fmt. Println ("ToLowerSpecial function usage ") Fmt. Println ("") Fmt. Println ("ToTitle function usage ") Fmt. Println (strings. ToTitle ("your noises ")) Fmt. Println (strings. ToTitle ("Hangzhou China ")) Fmt. Println ("") Fmt. Println ("Replace function usage ") Fmt. Println (strings. Replace ("ABAACEDF", "A", "a", 2) // aBaACEDF // If the fourth parameter is smaller than 0, all parameters are replaced. You can refer to golang's document. Fmt. Println (strings. Replace ("ABAACEDF", "A", "a",-1) // aBaaCEDF Fmt. Println ("") Fmt. Println ("ToUpper function usage ") Fmt. Println (strings. ToUpper ("Gopher") // GOPHER Fmt. Println ("") Fmt. Println ("Trim function usage ") Fmt. Printf ("[% q]", strings. Trim ("!!! Achtung !!! ","! ") // [" Achtung "] Fmt. Println ("") Fmt. Println ("TrimLeft function usage ") Fmt. Printf ("[% q]", strings. TrimLeft ("!!! Achtung !!! ","! ") // [" Achtung !!! "] Fmt. Println ("") Fmt. Println ("TrimSpace function usage ") Fmt. Println (strings. TrimSpace ("tn a lone gopher ntrn") // a lone gopher } |