Golang提供了許多內(nèi)置字符串函數(shù),可幫助您在處理字符串?dāng)?shù)據(jù)時(shí)執(zhí)行某些任務(wù)。
Golang字符串函數(shù)是核心部分。使用此功能無需安裝,僅需要導(dǎo)入“字符串”包。重要的Golang字符串函數(shù)列表如下:轉(zhuǎn)自:go語言中文文檔:www.
1)Golang字符串包含功能[區(qū)分大小寫]
您可以使用Contains()在字符串中搜索特定的文本/字符串/字符。它返回true或false的輸出。如果在字符串2中找到字符串1,則返回true。如果在字符串2中找不到字符串1,則返回false。
語法:
func Contains(s, substr string) bool
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("我是中國(guó)人", "中國(guó)")) //true
("I like golang", "like")) //true
("www.", "topgoer")) //true
("www.To;, "topgoer")) //false
("www.TopgoEr com", " ")) //true
}
輸出結(jié)果
true
true
true
false
true
2)Golang ContainsAny()[區(qū)分大小寫]
您可以使用ContainsAny()在字符串中搜索特定的文本/字符串/字符。它返回true或false的輸出。如果在字符串中找到字符的unicode,則它返回true,否則輸出將為false。您可以在下面的程序中看到ContainsAny與Contains的比較。
句法:
func ContainsAny(s,chars string)bool
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
Any("Golang", "g")) //true
Any("Golang", "l & a")) //true
Any("GolAng", "a")) // false
Any("Golang", "G")) //true
Any("GOLANG", "GOLANG")) //true
Any("GOLANG", "golang")) // false
Any("Shell-12541", "1")) //true
// Contains vs ContainsAny
Any("Shell-12541", "1-2")) // true
("Shell-12541", "1-2")) // false
}
輸出結(jié)果
true
true
false
true
true
false
true
true
false
3)Golang Count() [區(qū)分大小寫]
此函數(shù)計(jì)算字符串中字符/字符串/文本的不重疊實(shí)例的數(shù)量。
語法:
func Count(s,sep string)int
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("topgoer", "t")) //1
("Topgoer", "T")) //1
("Topgoer", "M")) //0
("Topgoer", "goer")) // 1
("Topgoer", "wwwTopgoercom")) // 0
("Shell-25152", "-25")) //1
("Shell-25152", "-21")) //0
("test", "")) // length of string + 1 5
("test", " ")) //0
}
輸出結(jié)果
1
1
0
1
0
1
0
5
0
4)Golang EqualFold() [不區(qū)分大小寫]
使用EqualFold,您可以檢查兩個(gè)字符串是否相等。如果兩個(gè)字符串相等,則返回輸出true,如果兩個(gè)字符串都不相等,則返回false。
語法
func EqualFold(s,t string)bool
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("Topgoer", "TOPGOER")) //true
("Topgoer", "topgoer")) //true
("Topgoer", "Topgoercom")) //false
("Topgoer", "goer")) //false
("Topgoer", "Topgoer & goer")) //false
("Topgoer-1254", "topgoer-1254")) //true
(" ", " ")) // single space both side //true
(" ", " ")) // double space right side //false
}
輸出結(jié)果
true
true
false
false
false
true
true
false
5) Golang Fields()
Fields函數(shù)將一個(gè)或多個(gè)連續(xù)空白字符的每個(gè)實(shí)例周圍的字符串分解為一個(gè)數(shù)組。
語法
func Fields(s string) []string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
testString := "I love my country"
testArray := (testString)
for _, v := range testArray {
(v)
}
}
輸出結(jié)果
I
love
my
country
6)Golang FieldsFunc()
FieldsFunc函數(shù)在每次運(yùn)行滿足f(c)的Unicode代碼點(diǎn)c時(shí)都將字符串s斷開,并返回s的切片數(shù)組。您可以使用此功能按數(shù)字或特殊字符的每個(gè)點(diǎn)分割字符串。檢查以下兩個(gè)FieldsFunc示例:
語法
func FieldsFunc(s string,f func(rune bool)[] string
DEMO
package main
import (
"fmt"
"strings"
"unicode"
)
func main() {
x := func(c rune) bool {
return !unicode.IsLetter(c)
}
strArray := Func(`I love my country – I,love?my!country
I, love, my – country`,x)
for _, v := range strArray {
(v)
}
("\n*****************Split by number*******************\n")
y := func(c rune) bool {
return unicode.IsNumber(c)
}
testArray := Func(`1 I love my coun I love my,coun I-love my coun I love my?country`,y)
for _, w := range testArray {
(w)
}
}
輸出結(jié)果
I
love
my
country
I
love
my
country
I
love
my
country
*****************Split by number*******************
I love my country.
I love my,country.
I-love my country.
I love my?country
7)Golang HasPrefix()
HasPrefix函數(shù)檢查字符串s是否以指定的字符串開頭。如果字符串S以前綴字符串開頭,則返回true,否則返回false。
語法
func HasPrefix(s, prefix string) bool
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("Topgoer", "Top")) //true
("Topgoer", "top")) //false
("Topgoer", "ccc")) //false
("Topgoer", "")) //true
}
輸出結(jié)果
true
false
false
true
8)Golang HasSuffix()
HasSuffix函數(shù)檢查字符串s是否以指定的字符串結(jié)尾。如果字符串S以后綴字符串結(jié)尾,則返回true,否則返回false。
語法
func HasSuffix(s, prefix string) bool
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("Topgoer", "goer")) //true
("Topgoer", "R")) //false
("Topgoer", "GOER")) //false
("123456", "456")) //true
("Topgoer", "")) //true
}
輸出結(jié)果
true
false
false
true
true
9)Golang Index()
Index功能可以搜索字符串中的特定文本。它僅通過匹配字符串中的特定文本即可工作。如果找到,則返回以0開頭的特定位置。如果找不到,則返回-1。
語法
func Index(s, sep string) int
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("Topgoer", "goer")) //true
("Topgoer", "R")) //false
("Topgoer", "GOER")) //false
("123-456", "-")) //true
("Topgoer", "")) //true
}
輸出結(jié)果
3
-1
-1
3
0
10)Golang IndexAny()
IndexAny函數(shù)從string [left]中的chars [right]返回任何Unicode代碼點(diǎn)的第一個(gè)實(shí)例的索引。它僅通過匹配字符串中的特定文本即可工作。如果找到,則返回以0開頭的特定位置。如果找不到,則返回-1。
語法:
func IndexAny(s,chars string)int
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
Any("topgoer", "www"))
Any("topgoer", "ggg"))
Any("mobile", "one"))
Any("123456789", "4"))
Any("123456789", "0"))
}
輸出結(jié)果
-1
3
1
3
-1
11)Golang IndexByte()
IndexByte函數(shù)返回字符串中第一個(gè)字符實(shí)例的索引。如果找到,則返回以0開頭的特定位置。如果找不到,則返回-1。
語法:
func IndexByte(s string, c byte) int
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
var s,t,u byte
t = 't'
Byte("Topgoer",t))
Byte("topgoer",t))
Byte("ogoer",t))
s = 1
Byte("5221-topgoer",s))
u = '1'
Byte("5221-topgoer",u))
}
輸出結(jié)果
-1
0
-1
-1
3
12)Golang IndexRune()
IndexRune函數(shù)以字符串形式返回Unicode代碼點(diǎn)r的第一個(gè)實(shí)例的索引。如果找到,則返回以0開頭的特定位置。如果找不到,則返回-1。在下面的示例中,s,t和u變量類型聲明為符文。
語法:
func IndexRune(s string, r rune) int
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
var s,t,u rune
t = 'T'
Rune("Topgoer",t))
Rune("topgoer",t))
Rune("opgoer",t))
s = 1
Rune("5221-JAPAN",s))
u = '1'
Rune("5221-JAPAN",u))
}
輸出結(jié)果
0
-1
-1
-1
3
13)Golang Join()[concatenate]
Join()函數(shù)從切片的元素返回字符串。Join將字符串Slice的元素連接起來以創(chuàng)建單個(gè)字符串。分隔符字符串sep指定在結(jié)果字符串中的切片元素之間放置的內(nèi)容。
語法
func Join(stringSlice []string, sep string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
// Slice of strings
textString := []string{"wen", "topgoer", "com"}
(textString, "-"))
// Slice of strings
textNum := []string{"1", "2", "3", "4", "5"}
(textNum,""))
}
輸出結(jié)果
wen-topgoer-com
12345
14)Golang LastIndex() [區(qū)分大小寫]
LastIndex函數(shù)可在字符串中搜索特定的特定文本/字符/字符串。它返回字符串中最后一個(gè)實(shí)例text / character / strin的索引。如果找到,則返回以0開頭的特定位置。如果找不到,則返回-1。
語法
func LastIndex(s, sep string) int
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("topgoer", "o")) // position j=0,a=1,p=2,a=3
("topgoer", "G"))
("Topgoer", "go"))
("TOPGOER TOPGOER", "go"))
("1234567890 1234567890", "0"))
("1234567890 1234567890", "00"))
("1234567890 1234567890", "123"))
}
輸出結(jié)果
4
-1
3
-1
20
-1
11
15)Golang Repeat()
Repeat函數(shù)將字符串重復(fù)指定的次數(shù),并返回一個(gè)新字符串,該字符串由字符串s的計(jì)數(shù)副本組成。Count指定將重復(fù)字符串的次數(shù)。必須大于或等于0。
語法
func Repeat(s string, count int) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
textString := "China"
repString := (textString, 5)
(repString)
textString = " A " // char with space on both side
repString = (textString,5)
(repString) // Repeat space also
("ba" + ("na", 2))
("111" + ("22", 2))
("111" + (" ",2))
}
輸出結(jié)果
ChinaChinaChinaChinaChina
A A A A A
banana
1112222
111
16)Golang Replace() [區(qū)分大小寫]
替換功能用字符串中的某些其他字符替換某些字符。n指定要在字符串中替換的字符數(shù)。如果n小于0,則替換次數(shù)沒有限制。
語法
func Replace(s, old, new string, n int) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("Australia Japan Canada Indiana", "an", "anese", 0))
("Australia Japan Canada Indiana", "an", "anese", 1))
("Australia Japan Canada Indiana", "an", "anese", 2))
("Australia Japan Canada Indiana", "an", "anese", -1))
("1234534232132", "1", "0", -1))
// case-sensitive
("Australia Japan Canada Indiana", "AN", "anese", -1))
}
輸出結(jié)果
Australia Japan Canada Indiana
Australia Japanese Canada Indiana
Australia Japanese Caneseada Indiana
Australia Japanese Caneseada Indianesea
17) Golang Split() [區(qū)分大小寫]
拆分功能將字符串拆分為一個(gè)切片。將s字符串拆分為用sep分隔的所有子字符串,并返回這些分隔符之間的子字符串的一部分。
語法
func Split(S string, sep string) []string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
strSlice := ("a,b,c", ",")
(strSlice,"\n")
strSlice = ("I love my country", " ")
for _, v := range strSlice {
(v)
}
strSlice = ("abacadaeaf","a")
("\n",strSlice)
strSlice = ("abacadaeaf","A")
("\n",strSlice)
strSlice = ("123023403450456056706780789","0")
("\n",strSlice)
strSlice = ("123023403450456056706780789",",")
("\n",strSlice)
}
輸出結(jié)果
[a b c]
I
love
my
country
[ b c d e f]
[abacadaeaf]
[123 234 345 456 567 678 789]
[123023403450456056706780789]
18)Golang SplitN() [區(qū)分大小寫]
SplitN函數(shù)將字符串分成片。SplitN將s字符串拆分為所有由sep分隔的子字符串,并返回這些分隔符之間的子字符串的一部分。n確定要返回的子字符串?dāng)?shù)。
n小于0:最多n個(gè)子字符串;最后一個(gè)子字符串將是未拆分的余數(shù)。
n等于0:結(jié)果為nil(零子字符串)
n大于0:所有子字符串
語法
func SplitN(s, sep string, n int) []string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
strSlice := N("a,b,c", ",",0)
(strSlice,"\n")
strSlice = N("a,b,c", ",",1)
(strSlice,"\n")
strSlice = N("a,b,c", ",",2)
(strSlice,"\n")
strSlice = N("a,b,c", ",",3)
(strSlice,"\n")
strSlice = N("I love my country", " ",-1)
for _, v := range strSlice {
(v)
}
strSlice = N("123023403450456056706780789","0",5)
("\n",strSlice)
}
輸出結(jié)果
[]
[a,b,c]
[a b,c]
[a b c]
I
love
my
country
[123 234 345 456 56706780789]
19)Golang SplitAfter() [區(qū)分大小寫]
SplitAfter函數(shù)將字符串分成片。在Sep的每個(gè)實(shí)例之后,SplitAfter將S切片為所有子字符串,并返回這些子字符串的切片。
語法
func SplitAfter(S String, sep string) []string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
strSlice := After("a,b,c", ",")
(strSlice,"\n")
strSlice = After("I love my country", " ")
for _, v := range strSlice {
(v)
}
strSlice = After("abacadaeaf","a")
("\n",strSlice)
strSlice = After("abacadaeaf","A")
("\n",strSlice)
strSlice = After("123023403450456056706780789","0")
("\n",strSlice)
strSlice = After("123023403450456056706780789",",")
("\n",strSlice)
}
輸出結(jié)果
[a, b, c]
I
love
my
country
[a ba ca da ea f]
[abacadaeaf]
[1230 2340 3450 4560 5670 6780 789]
[123023403450456056706780789]
20)Golang SplitAfterN()[區(qū)分大小寫]
SplitAfterN函數(shù)將字符串分成片。SplitAfterN在sep的每個(gè)實(shí)例之后將String切片為子字符串,并返回這些子字符串的切片。n確定要返回的子字符串?dāng)?shù)。
n小于0:最多n個(gè)子字符串;最后一個(gè)子字符串將是未拆分的余數(shù)。
n等于0:結(jié)果為nil(零子字符串)
n大于0:所有子字符串
語法
func SplitAfterN(string s, sep string, n int) []string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
strSlice := AfterN("a,b,c", ",",0)
(strSlice,"\n")
strSlice = AfterN("a,b,c", ",",1)
(strSlice,"\n")
strSlice = AfterN("a,b,c", ",",2)
(strSlice,"\n")
strSlice = AfterN("a,b,c", ",",3)
(strSlice,"\n")
strSlice = AfterN("I love my country", " ",-1)
for _, v := range strSlice {
(v)
}
strSlice = AfterN("123023403450456056706780789","0",5)
("\n",strSlice)
}
輸出結(jié)果
[]
[a,b,c]
[a, b,c]
[a, b, c]
I
love
my
country
[1230 2340 3450 4560 56706780789]
21)Golang Title()
Title函數(shù)將每個(gè)單詞的第一個(gè)字符轉(zhuǎn)換為大寫。
語法
func Title(s string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("i like golang"))
("i love my country"))
("topgoer com"))
}
輸出結(jié)果
I Like Golang
I Love My Country
Topgoer Com
22)Golang ToTitle()
ToTitle函數(shù)將每個(gè)單詞的所有字符轉(zhuǎn)換為大寫。
語法
func ToTitle(s string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("i like golang"))
("i love my country"))
("topgoer com"))
}
輸出結(jié)果
I LIKE GOLANG
I LOVE MY COUNTRY
TOPGOER COM
23)Golang ToLower()
ToTitle函數(shù)將每個(gè)單詞的所有字符轉(zhuǎn)換為小寫。
語法
func ToLower(s string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("I Like Golang"))
("I Love My Country"))
("topgoer Com"))
}
輸出結(jié)果
i like golang
i love my country
topgoer com
24)Golang ToUpper()
ToUpper函數(shù)將每個(gè)單詞的所有字符轉(zhuǎn)換為大寫。
語法
func ToUpper(s string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("I Like Golang"))
("I Love My Country"))
("topgoer Com"))
}
輸出結(jié)果
I LIKE GOLANG
I LOVE MY COUNTRY
TOPGOER COM
25)Golang Trim() [區(qū)分大小寫]
Trim函數(shù)從字符串的兩邊刪除預(yù)定義的字符cutset。
語法
func Trim(s string, cutset string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
("0120 2510", "0"))
("abcd axyz", "a"))
("abcd axyz", "A"))
("! Abcd dcbA !", "A"))
("! Abcd dcbA !", "!"))
(" Abcd dcbA ", " "))
}
輸出結(jié)果
120 251
bcd axyz
abcd axyz
! Abcd dcbA !
Abcd dcbA
Abcd dcbA
26)Golang TrimLeft() [區(qū)分大小寫]
trimlefit函數(shù)只從字符串s的左側(cè)刪除預(yù)定義字符cutset。
語法
func TrimLeft(s string, cutset string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
Left("0120 2510", "0"))
Left("abcd axyz", "a"))
Left("abcd axyz", "A"))
Left("! Abcd dcbA !", "A"))
Left("! Abcd dcbA !", "!"))
Left(" Abcd dcbA ", " "))
}
輸出結(jié)果
120 2510
bcd axyz
abcd axyz
! Abcd dcbA !
Abcd dcbA !
Abcd dcbA
27)Golang TrimRight() [區(qū)分大小寫]
TrimRight函數(shù)僅從字符串s的右側(cè)刪除預(yù)定義字符cutset。
語法
func TrimRight(s string, cutset string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
Right("0120 2510", "0"))
Right("abcd axyz", "a"))
Right("abcd axyz", "A"))
Right("! Abcd dcbA !", "A"))
Right("! Abcd dcbA !", "!"))
Right(" Abcd dcbA ", " "))
}
輸出結(jié)果
0120 251
abcd axyz
abcd axyz
! Abcd dcbA !
! Abcd dcbA
Abcd dcbA
28)Golang TrimSpace()
TrimSpace函數(shù)從字符串的兩側(cè)刪除空白和其他預(yù)定義字符。
“\t”-選項(xiàng)卡
“\n”-新行
“\x0B”-垂直選項(xiàng)卡
“\r”-回車
“”-普通空白
語法
func TrimSpace(s string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
Space(" I love my country "))
Space(" \t\n I love my country \t\n "))
Space(" \t\n\r\x0BI love my country\t\n "))
}
輸出結(jié)果
I love my country
I love my country
I love my country
29)Golang TrimPrefi() [區(qū)分大小寫]
TrimPrefix函數(shù)從S字符串的開頭刪除前綴字符串。如果S不以前綴開頭,則S將原封不動(dòng)地返回。
語法
func TrimPrefix(S string, prefix string) string
DEMO
package main
import (
"fmt"
"strings"
)
func main() {
var s string
s = "I love my country"
s = (s, "I")
s = (s)
(s)
s = "I love my country"
s = (s, "i")
(s)
s = "\nI-love-my-country"
s = (s, "\n")
(s)
s = "\tI-love-my-country"
s = (s, "\t")
(s)
}
輸出結(jié)果
love my country
I love my country
I-love-my-country
I-love-my-country
1.《123abab新域名專題之最受歡迎的Golang字符串內(nèi)置函數(shù)滿滿精華》援引自互聯(lián)網(wǎng),旨在傳遞更多網(wǎng)絡(luò)信息知識(shí),僅代表作者本人觀點(diǎn),與本網(wǎng)站無關(guān),侵刪請(qǐng)聯(lián)系頁腳下方聯(lián)系方式。
2.《123abab新域名專題之最受歡迎的Golang字符串內(nèi)置函數(shù)滿滿精華》僅供讀者參考,本網(wǎng)站未對(duì)該內(nèi)容進(jìn)行證實(shí),對(duì)其原創(chuàng)性、真實(shí)性、完整性、及時(shí)性不作任何保證。
3.文章轉(zhuǎn)載時(shí)請(qǐng)保留本站內(nèi)容來源地址,http://f99ss.com/guonei/2030239.html