Go language string processing

Source: Internet
Author: User
Tags gopher

String packet:            Whether the lookup string is in the specified strings: strings. Contains ("Seafood",  "foo")//true                              strings. Contains ("Seafood",  "bar")//false     lookup string is the number of occurrences in the specified strings: strings. Count ("Cheese",  "E")//3package mainimport  (         "FMT "        " Strings "        //" Unicode/utf8 ") Func main ()  {        fmt. Println ("Find whether the substring is in the specified string")         fmt. Println ("Use of  Contains  functions")         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  functions")         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  functions")         fmt. PRINTLN (Strings. Containsrune ("I am China",  ' I '))  // true  note the second argument, using the character          fmt. Println ("")         fmt. Println ("Use of  Count  functions")         fmt. PRINTLN (Strings. Count ("Cheese",  "E"))  // 3         fmt. PRINTLN (Strings. Count ("Five",  ""))     // before & after each rune result: 5 ,  source code has implemented &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FMT. Println ("")         fmt. Println ("Use of  EqualFold  functions")         fmt. PRINTLN (Strings. Equalfold ("Go",  "Go")  //casing ignores          fmt. Println ("")         fmt. Println ("Use of  Fields  functions")         fmt. Println ("Fields are: %q",  strings. Fields ("  foo bar  baz   "))  //["foo"   "bar"   "Baz"]   Returns a list         //equivalent to using functions as parameters, supporting anonymous functions          for _, record := range []string{" aaa*1892*122",  "Aaa\taa \ t ", " 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  functions")         fmt. PRINTLN (Strings. Hasprefix ("Nlt_abc",  "NLT"))  //prefixes are NLT that begin with &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FMT. Println ("")         fmt. Println ("Use of  HasSuffix  functions")         fmt. PRINTLN (Strings. Hassuffix ("NLT_ABC",  "abc"))  //suffix is NLT that begins with         fmt. Println ("")         fmt. Println ("Use of  Index  functions")         fmt. PRINTLN (Strings. Index ("NLT_ABC",  "abc")  //  returns the position of the first matching character, this is 4         Fmt. PRINTLN (Strings. Index ("Nlt_abc", "AAA"))  //  return  -1        fmt in existence. PRINTLN (Strings. Index ("I am Chinese",  ")")      //  in existence return  6         fmt. Println ("")         fmt. Println ("Use of  IndexAny  functions")         fmt. PRINTLN (Strings. Indexany ("I am Chinese",  "in"))  //  returns &NBSP;6&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FMT in existence. PRINTLN (Strings. Indexany ("I am Chinese",  "and"))  //  returns &NBSP;-1&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FMT in existence. Println ("")         fmt. Println ("Use of  Index  functions")         fmt. PRINTLN (Strings. Indexrune ("nlt_abc",  ' B '))  //  returns the position of the first matching character, here is the 4         Fmt. PRINTLN (Strings. Indexrune ("Nlt_abc",  ' s '))  //  return  -1   &nbsp in existence;     fmt. PRINTLN (Strings. Indexrune ("I am Chinese",  '))    //  in existence return  6         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 ("Use of  LastIndex  functions")         fmt. PRINTLN (Strings. LastIndex ("Go gopher",  "Go")  // 3        fmt. Println ("")         fmt. Println ("Use of  LastIndexAny  functions")         fmt. PRINTLN (Strings. LAstindexany ("Go gopher",  "Go")  // 4        fmt. PRINTLN (Strings. Lastindexany ("I am Chinese",  ")")       // 6         fmt. Println ("")         fmt. Println ("Use of  Map  Functions")         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  functions")         fmt. Println ("Ba"  + strings. Repeat ("NA",  2)) &NBSP;//BANANA&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FMT.println ("")         fmt. Println ("Use of  Replace  functions")         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  functions")         fmt. Printf ("%q\n",  strings. Split ("A,b,c",  ","))         fmt. Printf ("%q\n",  strings. Split ("A man a plan a canal panama",  "a "))          fmt. Printf ("%q\n",  strings. Split (" xyz ",  "))         fmt. Printf ("%q\n",  strings. Split (", " BerNardo o ' Higgins "))         fmt. Println ("")         fmt. Println ("Use of  SplitAfter  functions")         fmt. Printf ("%q\n",  strings. Splitafter ("/home/m_ta/src",  "/"))  //["/"   "home/"   "m_ta/"   "src"]         fmt. Println ("")         fmt. Println ("Use of  SplitAfterN  functions")         fmt. Printf ("%q\n",  strings. Splitaftern ("/home/m_ta/src",  "/",  2))   //["/"   "HOME/M_TA/SRC"]         fmt. Printf ("%q\n",  strings. Splitaftern ("#home #m_ta#src",  "#",  -1))  //["/"   "home/"   "m_ta/"   "src"]         fmt. Println ("")         fmt. Println ("Use of  SplitN  functions")          fmt. Printf ("%q\n",  strings. SPLITN ("/home/m_ta/src",  "/",  1))         fmt. Printf ("%q\n",  strings. SPLITN ("/home/m_ta/src",  "/",  2))   //["/"   "home/"   "m_ta/"   "src"]         fmt. Printf ("%q\n",  strings. SPLITN ("/home/m_ta/src",  "/",  -1))  //[""   "Home"   "M_ta"   "src"]         fmt. Printf ("%q\n",  strings. SPLITN ("Home,m_ta,src",  ",",  2))    //["/"   "home/"   "m_ta/"   "src"]         fmt. Printf ("%q\n",  strings. SPLITN ("#home #m_ta#src",  "#",  -1))  //["/"   "home/"   "m_ta/"   "src"]         fmt. Println ("")         fmt. PRINTLN ("The use of  Title  functions")  //This function, I really don't know what to use &nbsP;       fmt. PRINTLN (Strings. Title ("her royal highness"))         fmt. Println ("")         fmt. Println ("Use of  ToLower  functions")         fmt. PRINTLN (Strings. ToLower ("Gopher"))  //gopher         fmt. Println ("")         fmt. Println ("Use of  ToLowerSpecial  functions")         fmt. Println ("")         fmt. Println ("Use of  ToTitle  functions")         fmt. PRINTLN (Strings. Totitle ("loud noises"))         fmt. PRINTLN (Strings. Totitle ("loud  China"))         fmt. Println ("")         fmt. Println ("Use of  Replace  functionsLaw ")         fmt. PRINTLN (Strings. Replace ("Abaacedf",  "a",  "a",  2)  // aBaACEDF         //The fourth parameter is less than 0, which means that all of the replacement,  can look at the Golang document &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FMT. PRINTLN (Strings. Replace ("Abaacedf",  "a",  "a",  -1)  // aBaaCEDF         fmt. Println ("")         fmt. Println ("Use of  ToUpper  functions")         fmt. PRINTLN (Strings. ToUpper ("Gopher"))  //gopher        fmt. Println ("")         fmt. Println ("Use of  Trim   functions")         fmt. Printf ("[%q]",  strings. Trim (" !!!  Achtung !!!   ", "!  ")  // [" Achtung "]        fmt. Println ("")        fmt. Println ("Use of  TrimLeft  functions")         fmt. Printf ("[%q]",  strings. Trimleft (" !!!  Achtung !!!   ", "!  ")  // [" achtung !!!   "]&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FMT. Println ("")         fmt. Println ("Use of  TrimSpace  functions")         fmt. PRINTLN (Strings. Trimspace (" \t\n a lone gopher \n\t\r\n"))  // a lone gopher}

Go language string processing

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.