This is a creation in Article, where the information may have evolved or changed.
Strings is a very common type, here is a little summary of some common functions for reference.
1, strings. Split, Strings. Splitafter (), strings. Splitaftern (), strings. SPLITN (S,t,n)
Strings. Split~~strings. SPLITN (S,t,n)
Strings. Splitafter (), strings. Splitaftern ()
names:= "Nich~noel~geo~tur"
For _,name:=range strings. Split (names, "~") {
Fmt. Printf ("%s|", name)
}
Fmt. Println ()
Results:
Stringssplit ()----------------->nich| noel| geo| Tur
Stringssplitafter ()------------->nich~| noel~| geo~| Tur
Stringssplitaftern (String, "Sep", N)------------>n is the numbers of total substr. What are to say, if N==1 return Nich~noel~geo~tur n=2 return nich~| noel~geo~tur~ n=3 return nich~| noel~| geo~tur~
2, strings. Fieldsfunc () can slice two or more characters
For _,name: = range []string{"Ad*d*c*d", "ES\TSD\TSF", "Ma|sds|fwe23"} {
Fmt. PRINTLN (Strings. Fieldsfunc (Name,func (Char rune) bool{
Switch Char {
Case ' \ t ', ' * ', ' | ':
return True
}
return False
}))
}
3, Strings. Replace
Names:=strings. Replace (names, "\ T", "" ", N): N is the number of times to be replaced, n=-1 means no limit, replace all
Others
4, strings. Contains (s,t) if T in S return true
5. Strings. Count (s,t) counts of T appear in S
6. Strings. Fields (s) split by white space
Strings. Join (XS,T) join total strings of Xs by T
7. Strings. Hasprefix (s,t) if S is the header of T return True
8. Strings. Hassuffix (s,t) If S is the end of T return True
Strings. Newreader (s) return the string S,s has the method of Read, ReadByte and Readrune
Strings. Newreplacer (...)
Strings. Repeat (s,i) Repeat s with times I
30.strings. MAP (mf,t) Replace string t by the function rule func (rune) Rune
Index
9. Strings. Index (s,t) return the site that T first appears in S
Ten. Strings. Indexany (s,t) t,s is both strings and they share some common word, return the index position that word of T first app Ears in S
One by one. Strings. Indexfunc (s,f) return index that word in s first making F returning True
Strings. Indexrune (S,char) return the index that Char first appears in S
Strings. LastIndex (s,t) return the last index of T appears in S
Strings. Lastindexany (S,T) return the index, the word of T last appears in S
Strings. Lastindexfunc (S,T) return the index, the word in t last making F returning true
Upper and Lowwer
Strings. Title (s) return a new strings s that make the first letter of word in old strings Upper
Strings. ToLower (s) return a new strings s lowwer
. String. ToUpper (s) return a new strings s Upper
Trim
Strings. Trim (S,T) return a new strings which filt T from the left and right side of s
Strings. Trimleft (S,T) return a new strings which filt T from the left of S
Strings. TrimRight (S,T) return a new strings which filt T from the right of s
Strings. Trimfunc (S,F) return a new strings which the word of S make function f returning true from the left and right side of s
Strings. Trimleftfunc (S,F) return a new strings which the word of S make function f returning true from the left side of S
Strings. Trimrightfunc (S,F) return a new strings which the word of S make function f returning true from the right side of s
Strings. Trimspace (s) return a new strings which filt space from the right of s
s:= "HJLSHJLKHJ" if->return lshjlk, return LSHJLKHJ
T:= "HJ"