Go common String function operation example

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

Example of the use of the Go language string function


Package Mainimport ("FMT" "StrConv" "strings") func main () {//String using Str_sample () fmt. Println ("------------------------------------")//strings package function is used. String_function_sample ()}/* * String Use example * Author:roadtotheexpert */func str_sample () {str: = "Hello" str + = "World" FMT. PRINTLN ("String addition:", str) FMT. Println ("Calculate string length:", Len (str)) FMT. Printf ("Get corresponding subscript characters:%c%d \ n", Str[0], str[1])//String traversal r: = []rune (str + "China") fmt. PRINTLN ("Unicode-compatible string traversal:") for I: = 0; I < Len (R); i++ {fmt. Println (String (R[i]), R[i])}//string to Integer n, err: = StrConv. Atoi ("a") if Err = = Nil {fmt. Printf ("string-to-integer:%d \ n",}//) integer-to-string FMT. Println ("Integer to String:", StrConv.) Itoa (12345))//String goto []BYTEFMT. Println ("String goto []byte:", []byte ("Hello Go"))//[]byte to the string fmt. Println ("[]byte to String:", String ([]byte{51, 52, 53, 54, 55, 56}))//There may be a Unicode problem//16 in the FMT. Println ("Hex:", StrConv. Formatint (Int64 (28), 16)//binary FMT. Printf ("binary:") fmt. Println (StrConv. Formatint (123, 2))//hexadecimal string to Integer n2, _: = StrConv. parseint ("be", +--) fmt. Printf ("hexadecimal string to integer:%d \ n", inT (n2))//format string FMT. Println (FMT. Sprintf ("formatted string 0x%X", "$)")}/* * Strings package function is used. */func string_function_sample () {//The function code is collected from the Internet 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 a ChinesePerson "," middle "))//in existence return 6FMT. PRINTLN (Strings. Indexany ("I am Chinese", "and"))//In the presence of return -1FMT. Println ("") fmt. Println ("Use of the Index function") 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 ("Use of the 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 ("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.