Golang String Manipulation instances

Source: Internet
Author: User
Tags gopher
This is a creation in Article, where the information may have evolved or changed.

The code is as follows:

Package Mainimport S "strings"//Alias Import ("Fmt") var p = fmt. Printlnfunc Main () {p ("Contains:", S.contains ("test", "es")//Whether it contains TRUEP ("Count:", S.count ("test", "T"))//string occurrences of the number of characters 2 P ("Hasprefix:", S.hasprefix ("Test", "TE"))//Judgment String header Truep ("Hassuffix:", S.hassuffix ("Test", "St"))//Judgment string end Truep (" Index: ", S.index (" Test "," E "))//query string position 1p (" Join: ", S.join ([]string{" A "," B "},"-"))//string array connection a-bp (" Repeat: ", S.repea T ("a", 5))//Repeats a string Aaaaap ("Replace:", S.replace ("foo", "O", "0",-1))//String substitution specifies that the starting position is less than 0, then replace all f00p ("Replace:", S.REPL Ace ("foo", "O", "0", 1))//String substitution specifies the starting position 1 F0op ("Split:", S.split ("A-b-c-d-e", "-"))//String cut [a b c D e]p ("ToLower:", s.to Lower ("test"))//String lowercase conversion testp ("ToUpper:", S.toupper ("test"))//String capitalization conversion TESTP () p ("Len:", Len ("Hello"))//String length p ("Char : "," Hello "[1])//The character in the string, type Byte}



The detailed usage is as follows:

Package Mainimport ("FMT" "Strings"//"Unicode/utf8") func main () {FMT. Println ("Find whether the substring is in the specified string") Fmt. PRINTLN ("Usage of the Contains function") fmt. PRINTLN (Strings. Contains ("Seafood", "foo"))//truefmt. PRINTLN (Strings. Contains ("Seafood", "Bar"))//falsefmt. PRINTLN (Strings. Contains ("Seafood", ""))//truefmt. PRINTLN (Strings. Contains ("", ""))//true here to pay special attention to the FMT. PRINTLN (Strings. Contains ("I am Chinese", "I"))//truefmt. Println ("") fmt. PRINTLN ("Usage of the Containsany function") fmt. PRINTLN (Strings. Containsany ("Team", "I")//FALSEFMT. PRINTLN (Strings. Containsany ("Failure", "U & I"))//TRUEFMT. PRINTLN (Strings. Containsany ("foo", ""))//FALSEFMT. PRINTLN (Strings. Containsany ("", ""))//FALSEFMT. Println ("") fmt. PRINTLN ("Usage of the Containsrune function") fmt. PRINTLN (Strings. Containsrune ("I am China", ' I '))//True note the second parameter, with the character Fmt. Println ("") fmt. PRINTLN ("Usage of the Count function") fmt. PRINTLN (Strings. Count ("Cheese", "e"))//3FMT. PRINTLN (Strings. Count ("Five", ""))//Before & after each rune Result:5, the source code has implemented FMT. Println ("") fmt. PRINTLN ("Usage of the Equalfold function") fmt. PrinTLN (Strings. Equalfold ("Go", "go")//Case ignores FMT. Println ("") fmt. Println ("Use of the Fields function") fmt. Println ("Fields is:%q", strings. Fields ("foo bar baz"))//["foo" "Bar" "Baz"] returns a list//equivalent function as parameter, supports anonymous function 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 ("Usage of the Hasprefix function") fmt. PRINTLN (Strings. The Hasprefix ("Nlt_abc", "NLT"))//prefix is the FMT.PRINTLN ("") FMT that begins with NLT. PRINTLN ("Usage of the Hassuffix function") fmt. PRINTLN (Strings. Hassuffix ("Nlt_abc", "abc")//suffix is the fmt.println ("") FMT beginning with NLT. Println ("Use of the Index function") fmt. PRINTLN (Strings. Index ("Nlt_abc", "abc"))//Returns the position of the first matching character, here is 4fmt. PRINTLN (Strings. Index ("Nlt_abc", "AAA")//returns -1FMT in existence. PRINTLN (Strings. Index ("I am Chinese", "Medium"))//in existence return 6FMT. Println ("") fmt. PRINTLN ("Usage of the Indexany function") fmt. PRINTLN (Strings. Indexany ("I am Chinese", "Medium"))//in existence returns 6FMT. PRINTLN (Strings. Indexany ("I am Chinese", "and"))//In the presence of return -1FMT. Println ("") fmt. PRINTLN ("Index LetterThe use of the number ") Fmt. PRINTLN (Strings. Indexrune ("Nlt_abc", ' B '))//Returns the position of the first matching character, here is 4fmt. PRINTLN (Strings. Indexrune ("Nlt_abc", ' s '))//returns -1FMT in existence. PRINTLN (Strings. Indexrune ("I am Chinese", "Middle"))//in existence return 6FMT. Println ("") fmt. PRINTLN ("Usage of Join function") s: = []string{"foo", "Bar", "Baz"}fmt. PRINTLN (Strings. Join (S, ","))//return string: Foo, bar, Bazfmt. Println ("") fmt. PRINTLN ("Usage of the LastIndex function") fmt. PRINTLN (Strings. LastIndex ("Go Gopher", "go")//3FMT. Println ("") fmt. PRINTLN ("Usage of the Lastindexany function") fmt. PRINTLN (Strings. Lastindexany ("Go Gopher", "go")//4FMT. PRINTLN (Strings. Lastindexany ("I am Chinese", "Medium"))//6FMT. Println ("") fmt. Println ("Use of the Map function") rot13: = func (R rune) Rune {switch {case R >= ' A ' && r <= ' Z ': return ' a ' + (R ' a ' +13)%2 6case 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 ("Usage of the Repeat function") fmt. Println ("ba" + Strings. Repeat ("NA", 2))//bananafmt. Println ("") fmt. PRINTLN ("Replace LetterThe use of the number ") Fmt. PRINTLN (Strings. Replace ("Oink oink Oink", "K", "KY", 2)) fmt. PRINTLN (Strings. Replace ("Oink oink oink", "Oink", "moo",-1)) FMT. Println ("") fmt. PRINTLN ("The use of the 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 ("Usage of the Splitafter function") fmt. Printf ("%qn", strings. Splitafter ("/home/m_ta/src", "/"))//["/" "home/" "m_ta/" "src"]fmt. Println ("") fmt. PRINTLN ("Usage of the 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 ("Usage of the 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 ("The use of the Title function")//This function, and really do not know what to use in FMT. PRINTLN (Strings. Title ("Her Royal Highness") Fmt. Println ("") fmt. PRINTLN ("Usage of the ToLower function") fmt. PRINTLN (Strings. ToLower ("Gopher"))//gopherfmt. Println ("") fmt. PRINTLN ("Usage of the Tolowerspecial function") fmt. Println ("") fmt. PRINTLN ("Usage of the Totitle function") fmt. PRINTLN (Strings. Totitle ("Loud noises")) fmt. PRINTLN (Strings. Totitle ("Loud China")) FMT. Println ("") fmt. Println ("Use of the Replace function") fmt. PRINTLN (Strings. Replace ("Abaacedf", "a", "a", 2))//abaacedf//The fourth parameter is less than 0, which means that all of the substitutions can be seen under the Golang document FMT. PRINTLN (Strings. Replace ("Abaacedf", "a", "a",-1))//Abaacedffmt.println ("") fmt. PRINTLN ("Usage of the ToUpper function") fmt. PRINTLN (Strings. ToUpper ("Gopher"))//gopherfmt.println ("") fmt. Println ("Use of the Trim function") fmt. Printf ("[%q]", strings. Trim ("!!! Achtung!!! ", "! ")) ["AchtUng "]fmt. Println ("") fmt. PRINTLN ("Usage of the Trimleft function") fmt. Printf ("[%q]", strings. Trimleft ("!!! Achtung!!! ", "! ")) ["Achtung!!!"] Fmt. Println ("") fmt. PRINTLN ("Usage of the Trimspace function") fmt. PRINTLN (Strings. Trimspace ("tn a lone Gopher ntrn")//A lone Gopher}



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.