Introduction to Golang string manipulation examples

Source: Internet
Author: User
Tags gopher trim


Example 1

The code is as follows Copy Code

Package Main

Import S "Strings"

Alias
Import (
"FMT"
)

var p = fmt. Println

Func Main () {
P ("Contains:", S.contains ("test", "es"))//contains True
P ("Count:", S.count ("test", "T"))//string occurrences of characters 2
P ("Hasprefix:", S.hasprefix ("Test", "TE"))//Judge String header True
P ("Hassuffix:", S.hassuffix ("Test", "St"))//Judgment string end True
P ("Index:", S.index ("Test", "E"))//query string position 1
P ("Join:", S.join ([]string{"A", "B"}, "-"))/string array connection A-b
P ("Repeat:", S.repeat ("a", 5))//Repeat a string aaaaa
P ("Replace:", S.replace ("foo", "O", "0",-1))//String substitution specifies that the starting position is less than 0, then replace all f00
P ("Replace:", S.replace ("foo", "O", "0", 1))//string substitution Specify starting position 1 f0o
P ("Split:", S.split ("A-b-c-d-e", "-"))//String cutting [a b c D e]
P ("ToLower:", S.tolower ("TEST"))//String lowercase conversion TEST
P ("ToUpper:", S.toupper ("test"))//String uppercase conversion test
P ()
P ("Len:", Len ("Hello"))//String length
P ("Char:", "Hello" [1])///character in string, type Byte
}

Cases

The code is as follows Copy Code

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

}

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.