String
2021.07

Golang提供了许多内置的字符串函数,这些函数可在处理字符串数据时帮助执行一些操作。Golang字符串函数是核心部分。使用此功能无需安装,仅需要导入“字符串”包。重要的Golang字符串函数列表如下:

1)Golang字符串包含功能[区分大小写]

您可以使用Contains()在字符串中搜索特定的文本/字符串/字符。它返回true或false的输出。如果在字符串2中找到字符串1,则返回true。如果在字符串2中找不到字符串1,则返回false。

语法:

func Contains(s, substr string) bool

DEMO

package main
import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Contains("我是中国人", "中国"))   //true
    fmt.Println(strings.Contains("I like golang", "like"))   //true
    fmt.Println(strings.Contains("www.topgoer.com", "topgoer"))  //true
    fmt.Println(strings.Contains("www.TopgoEr.com", "topgoer"))  //false
    fmt.Println(strings.Contains("www.TopgoEr com", " "))   //true
}

输出结果

true
true
true
false
true

2)Golang ContainsAny()[区分大小写]

您可以使用ContainsAny()在字符串中搜索特定的文本/字符串/字符。它返回true或false的输出。如果在字符串中找到字符的unicode,则它返回true,否则输出将为false。您可以在下面的程序中看到ContainsAny与Contains的比较。

句法:

func ContainsAny(s,chars string)bool

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ContainsAny("Golang", "g"))    //true
    fmt.Println(strings.ContainsAny("Golang", "l & a"))  //true
    fmt.Println(strings.ContainsAny("GolAng", "a"))      // false
    fmt.Println(strings.ContainsAny("Golang", "G"))     //true
    fmt.Println(strings.ContainsAny("GOLANG", "GOLANG"))   //true
    fmt.Println(strings.ContainsAny("GOLANG", "golang"))   // false
    fmt.Println(strings.ContainsAny("Shell-12541", "1"))  //true
    //  Contains vs ContainsAny
    fmt.Println(strings.ContainsAny("Shell-12541", "1-2")) // true
    fmt.Println(strings.Contains("Shell-12541", "1-2"))   // false
}

输出结果

true
true
false
true
true
false
true
true
false

3)Golang Count() [区分大小写]

此函数计算字符串中字符/字符串/文本的不重叠实例的数量。

语法:

func Count(s,sep string)int

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Count("topgoer", "t"))   //1
    fmt.Println(strings.Count("Topgoer", "T"))   //1
    fmt.Println(strings.Count("Topgoer", "M"))   //0
    fmt.Println(strings.Count("Topgoer", "goer"))   // 1
    fmt.Println(strings.Count("Topgoer", "wwwTopgoercom"))   // 0
    fmt.Println(strings.Count("Shell-25152", "-25"))  //1
    fmt.Println(strings.Count("Shell-25152", "-21"))  //0
    fmt.Println(strings.Count("test", ""))    // length of string + 1   5
    fmt.Println(strings.Count("test", " "))   //0
}

输出结果

1
1
0
1
0
1
0
5
0

4)Golang EqualFold() [不区分大小写]

使用EqualFold,您可以检查两个字符串是否相等。如果两个字符串相等,则返回输出true,如果两个字符串都不相等,则返回false。

语法

func EqualFold(s,t string)bool

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.EqualFold("Topgoer", "TOPGOER"))   //true
    fmt.Println(strings.EqualFold("Topgoer", "topgoer"))   //true
    fmt.Println(strings.EqualFold("Topgoer", "Topgoercom"))  //false
    fmt.Println(strings.EqualFold("Topgoer", "goer"))      //false
    fmt.Println(strings.EqualFold("Topgoer", "Topgoer & goer"))   //false
    fmt.Println(strings.EqualFold("Topgoer-1254", "topgoer-1254"))  //true
    fmt.Println(strings.EqualFold(" ", " "))  // single space both side   //true
    fmt.Println(strings.EqualFold(" ", "  "))  // double space right side  //false
}

输出结果

true
true
false
false
false
true
true
false

5) Golang Fields()

Fields函数将一个或多个连续空白字符的每个实例周围的字符串分解为一个数组。

语法

func Fields(s string) []string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    testString := "I love my country"
    testArray := strings.Fields(testString)
    for _, v := range testArray {
        fmt.Println(v)
    }
}

输出结果

I
love
my
country

6)Golang FieldsFunc()

FieldsFunc函数在每次运行满足f(c)的Unicode代码点c时都将字符串s断开,并返回s的切片数组。您可以使用此功能按数字或特殊字符的每个点分割字符串。检查以下两个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 := strings.FieldsFunc(`I love my country – I,love?my!country
                                 I, love, my – country`,x)
    for _, v := range strArray {
        fmt.Println(v)
    }

    fmt.Println("\n*****************Split by number*******************\n")

    y := func(c rune) bool {
        return unicode.IsNumber(c)
    }
    testArray := strings.FieldsFunc(`1 I love my country.2 I love my,country.3 I-love my country.4 I love my?country`,y)
    for _, w := range testArray {
        fmt.Println(w)
    }
}

输出结果

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函数检查字符串s是否以指定的字符串开头。如果字符串S以前缀字符串开头,则返回true,否则返回false。

语法

func HasPrefix(s, prefix string) bool

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.HasPrefix("Topgoer", "Top"))    //true
    fmt.Println(strings.HasPrefix("Topgoer", "top"))    //false
    fmt.Println(strings.HasPrefix("Topgoer", "ccc"))    //false
    fmt.Println(strings.HasPrefix("Topgoer", ""))      //true
}

输出结果

true
false
false
true

8)Golang HasSuffix()

HasSuffix函数检查字符串s是否以指定的字符串结尾。如果字符串S以后缀字符串结尾,则返回true,否则返回false。

语法

func HasSuffix(s, prefix string) bool

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.HasSuffix("Topgoer", "goer"))   //true
    fmt.Println(strings.HasSuffix("Topgoer", "R"))      //false
    fmt.Println(strings.HasSuffix("Topgoer", "GOER"))   //false
    fmt.Println(strings.HasSuffix("123456", "456"))   //true
    fmt.Println(strings.HasSuffix("Topgoer", ""))     //true
}

输出结果

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() {
    fmt.Println(strings.Index("Topgoer", "goer"))   //true
    fmt.Println(strings.Index("Topgoer", "R"))      //false
    fmt.Println(strings.Index("Topgoer", "GOER"))   //false
    fmt.Println(strings.Index("123-456", "-"))   //true
    fmt.Println(strings.Index("Topgoer", ""))     //true
}

输出结果

3
-1
-1
3
0

10)Golang IndexAny()

IndexAny函数从string [left]中的chars [right]返回任何Unicode代码点的第一个实例的索引。它仅通过匹配字符串中的特定文本即可工作。如果找到,则返回以0开头的特定位置。如果找不到,则返回-1。

语法:

func IndexAny(s,chars string)int

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.IndexAny("topgoer", "www"))
    fmt.Println(strings.IndexAny("topgoer", "ggg"))
    fmt.Println(strings.IndexAny("mobile", "one"))
    fmt.Println(strings.IndexAny("123456789", "4")) 
    fmt.Println(strings.IndexAny("123456789", "0"))
}

输出结果

-1
3
1
3
-1

11)Golang IndexByte()

IndexByte函数返回字符串中第一个字符实例的索引。如果找到,则返回以0开头的特定位置。如果找不到,则返回-1。

语法:

func IndexByte(s string, c byte) int

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    var s,t,u byte
    t = 't'
    fmt.Println(strings.IndexByte("Topgoer",t))
    fmt.Println(strings.IndexByte("topgoer",t))
    fmt.Println(strings.IndexByte("ogoer",t))
    s = 1
    fmt.Println(strings.IndexByte("5221-topgoer",s))
    u = '1'
    fmt.Println(strings.IndexByte("5221-topgoer",u))
}

输出结果

-1
0
-1
-1
3

12)Golang IndexRune()

IndexRune函数以字符串形式返回Unicode代码点r的第一个实例的索引。如果找到,则返回以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'
    fmt.Println(strings.IndexRune("Topgoer",t))
    fmt.Println(strings.IndexRune("topgoer",t))
    fmt.Println(strings.IndexRune("opgoer",t))
    s = 1
    fmt.Println(strings.IndexRune("5221-JAPAN",s))
    u = '1'
    fmt.Println(strings.IndexRune("5221-JAPAN",u))
}

输出结果

0
-1
-1
-1
3

13)Golang Join()[concatenate]

Join()函数从切片的元素返回字符串。Join将字符串Slice的元素连接起来以创建单个字符串。分隔符字符串sep指定在结果字符串中的切片元素之间放置的内容。

语法

func Join(stringSlice []string, sep string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    // Slice of strings
    textString := []string{"wen", "topgoer", "com"}
    fmt.Println(strings.Join(textString, "-"))

    // Slice of strings
    textNum := []string{"1", "2", "3", "4", "5"}
    fmt.Println(strings.Join(textNum,""))
}

输出结果

wen-topgoer-com
12345

14)Golang LastIndex() [区分大小写]

LastIndex函数可在字符串中搜索特定的特定文本/字符/字符串。它返回字符串中最后一个实例text / character / strin的索引。如果找到,则返回以0开头的特定位置。如果找不到,则返回-1。

语法

func LastIndex(s, sep string) int

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.LastIndex("topgoer", "o")) // position j=0,a=1,p=2,a=3
    fmt.Println(strings.LastIndex("topgoer", "G"))
    fmt.Println(strings.LastIndex("Topgoer", "go"))
    fmt.Println(strings.LastIndex("TOPGOER TOPGOER", "go"))
    fmt.Println(strings.LastIndex("1234567890 1234567890", "0"))
    fmt.Println(strings.LastIndex("1234567890 1234567890", "00"))
    fmt.Println(strings.LastIndex("1234567890 1234567890", "123"))
}

输出结果

4
-1
3
-1
20
-1
11

15)Golang Repeat()

Repeat函数将字符串重复指定的次数,并返回一个新字符串,该字符串由字符串s的计数副本组成。Count指定将重复字符串的次数。必须大于或等于0。

语法

func Repeat(s string, count int) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    textString := "China"
    repString := strings.Repeat(textString, 5)
    fmt.Println(repString)

    textString = " A " // char with space on both side
    repString = strings.Repeat(textString,5)
    fmt.Println(repString)        // Repeat space also

    fmt.Println("ba" + strings.Repeat("na", 2))
    fmt.Println("111" + strings.Repeat("22", 2))
    fmt.Println("111" + strings.Repeat(" ",2))
}

输出结果

ChinaChinaChinaChinaChina
 A  A  A  A  A
banana
1112222
111

16)Golang Replace() [区分大小写]

替换功能用字符串中的某些其他字符替换某些字符。n指定要在字符串中替换的字符数。如果n小于0,则替换次数没有限制。

语法

func Replace(s, old, new string, n int) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {

    fmt.Println(strings.Replace("Australia Japan Canada Indiana", "an", "anese", 0))
    fmt.Println(strings.Replace("Australia Japan Canada Indiana", "an", "anese", 1))
    fmt.Println(strings.Replace("Australia Japan Canada Indiana", "an", "anese", 2))
    fmt.Println(strings.Replace("Australia Japan Canada Indiana", "an", "anese", -1))
    fmt.Println(strings.Replace("1234534232132", "1", "0", -1))
    // case-sensitive
    fmt.Println(strings.Replace("Australia Japan Canada Indiana", "AN", "anese", -1))
}

输出结果

Australia Japan Canada Indiana
Australia Japanese Canada Indiana
Australia Japanese Caneseada Indiana
Australia Japanese Caneseada Indianesea

17) Golang Split() [区分大小写]

拆分功能将字符串拆分为一个切片。将s字符串拆分为用sep分隔的所有子字符串,并返回这些分隔符之间的子字符串的一部分。

语法

func Split(S string, sep string) []string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    strSlice := strings.Split("a,b,c", ",")
    fmt.Println(strSlice,"\n")

    strSlice = strings.Split("I love my country", " ")
    for _, v := range strSlice {
        fmt.Println(v)
    }

    strSlice = strings.Split("abacadaeaf","a")
    fmt.Println("\n",strSlice)

    strSlice = strings.Split("abacadaeaf","A")
    fmt.Println("\n",strSlice)

    strSlice = strings.Split("123023403450456056706780789","0")
    fmt.Println("\n",strSlice)

    strSlice = strings.Split("123023403450456056706780789",",")
    fmt.Println("\n",strSlice)
}

输出结果

[a b c]

I
love
my
country

 [ b c d e f]

 [abacadaeaf]

 [123 234 345 456 567 678 789]

 [123023403450456056706780789]

18)Golang SplitN() [区分大小写]

SplitN函数将字符串分成片。SplitN将s字符串拆分为所有由sep分隔的子字符串,并返回这些分隔符之间的子字符串的一部分。n确定要返回的子字符串数。n小于0:最多n个子字符串;最后一个子字符串将是未拆分的余数。n等于0:结果为nil(零子字符串)n大于0:所有子字符串

语法

func SplitN(s, sep string, n int) []string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    strSlice := strings.SplitN("a,b,c", ",",0)
    fmt.Println(strSlice,"\n")

    strSlice = strings.SplitN("a,b,c", ",",1)
    fmt.Println(strSlice,"\n")

    strSlice = strings.SplitN("a,b,c", ",",2)
    fmt.Println(strSlice,"\n")

    strSlice = strings.SplitN("a,b,c", ",",3)
    fmt.Println(strSlice,"\n")

    strSlice = strings.SplitN("I love my country", " ",-1)
    for _, v := range strSlice {
        fmt.Println(v)
    }
    strSlice = strings.SplitN("123023403450456056706780789","0",5)
    fmt.Println("\n",strSlice)
}

输出结果

[]

[a,b,c]

[a b,c]

[a b c]

I
love
my
country

 [123 234 345 456 56706780789]

19)Golang SplitAfter() [区分大小写]

SplitAfter函数将字符串分成片。在Sep的每个实例之后,SplitAfter将S切片为所有子字符串,并返回这些子字符串的切片。

语法

func SplitAfter(S String, sep string) []string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    strSlice := strings.SplitAfter("a,b,c", ",")
    fmt.Println(strSlice,"\n")

    strSlice = strings.SplitAfter("I love my country", " ")
    for _, v := range strSlice {
        fmt.Println(v)
    }

    strSlice = strings.SplitAfter("abacadaeaf","a")
    fmt.Println("\n",strSlice)

    strSlice = strings.SplitAfter("abacadaeaf","A")
    fmt.Println("\n",strSlice)

    strSlice = strings.SplitAfter("123023403450456056706780789","0")
    fmt.Println("\n",strSlice)

    strSlice = strings.SplitAfter("123023403450456056706780789",",")
    fmt.Println("\n",strSlice)
}

输出结果

[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()[区分大小写]

SplitAfterN函数将字符串分成片。SplitAfterN在sep的每个实例之后将String切片为子字符串,并返回这些子字符串的切片。n确定要返回的子字符串数。n小于0:最多n个子字符串;最后一个子字符串将是未拆分的余数。n等于0:结果为nil(零子字符串)n大于0:所有子字符串

语法

func SplitAfterN(string s, sep string, n int) []string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    strSlice := strings.SplitAfterN("a,b,c", ",",0)
    fmt.Println(strSlice,"\n")

    strSlice = strings.SplitAfterN("a,b,c", ",",1)
    fmt.Println(strSlice,"\n")

    strSlice = strings.SplitAfterN("a,b,c", ",",2)
    fmt.Println(strSlice,"\n")

    strSlice = strings.SplitAfterN("a,b,c", ",",3)
    fmt.Println(strSlice,"\n")

    strSlice = strings.SplitAfterN("I love my country", " ",-1)
    for _, v := range strSlice {
        fmt.Println(v)
    }
    strSlice = strings.SplitAfterN("123023403450456056706780789","0",5)
    fmt.Println("\n",strSlice)
}

输出结果

[]

[a,b,c]

[a, b,c]

[a, b, c]

I
love
my
country

 [1230 2340 3450 4560 56706780789]

21)Golang Title()

Title函数将每个单词的第一个字符转换为大写。

语法

func Title(s string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Title("i like golang"))
    fmt.Println(strings.Title("i love my country"))
    fmt.Println(strings.Title("topgoer com"))
}

输出结果

I Like Golang
I Love My Country
Topgoer Com

22)Golang ToTitle()

ToTitle函数将每个单词的所有字符转换为大写。

语法

func ToTitle(s string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ToTitle("i like golang"))
    fmt.Println(strings.ToTitle("i love my country"))
    fmt.Println(strings.ToTitle("topgoer com"))
}

输出结果

I LIKE GOLANG
I LOVE MY COUNTRY
TOPGOER COM

23)Golang ToLower()

ToTitle函数将每个单词的所有字符转换为小写。

语法

func ToLower(s string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ToLower("I Like Golang"))
    fmt.Println(strings.ToLower("I Love My Country"))
    fmt.Println(strings.ToLower("topgoer Com"))
}

输出结果

i like golang
i love my country
topgoer com

24)Golang ToUpper()

ToUpper函数将每个单词的所有字符转换为大写。

语法

func ToUpper(s string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.ToUpper("I Like Golang"))
    fmt.Println(strings.ToUpper("I Love My Country"))
    fmt.Println(strings.ToUpper("topgoer Com"))
}

输出结果

I LIKE GOLANG
I LOVE MY COUNTRY
TOPGOER COM

25)Golang Trim() [区分大小写]

Trim函数从字符串的两边删除预定义的字符cutset。

语法

func Trim(s string, cutset string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Trim("0120 2510", "0"))
    fmt.Println(strings.Trim("abcd axyz", "a"))
    fmt.Println(strings.Trim("abcd axyz", "A"))
    fmt.Println(strings.Trim("! Abcd dcbA !", "A"))
    fmt.Println(strings.Trim("! Abcd dcbA !", "!"))
    fmt.Println(strings.Trim(" Abcd dcbA ", " "))
}

输出结果

120 251
bcd axyz
abcd axyz
! Abcd dcbA !
 Abcd dcbA
Abcd dcbA

26)Golang TrimLeft() [区分大小写]

trimlefit函数只从字符串s的左侧删除预定义字符cutset。

语法

func TrimLeft(s string, cutset string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.TrimLeft("0120 2510", "0"))
    fmt.Println(strings.TrimLeft("abcd axyz", "a"))
    fmt.Println(strings.TrimLeft("abcd axyz", "A"))
    fmt.Println(strings.TrimLeft("! Abcd dcbA !", "A"))
    fmt.Println(strings.TrimLeft("! Abcd dcbA !", "!"))
    fmt.Println(strings.TrimLeft(" Abcd dcbA ", " "))
}

输出结果

120 2510
bcd axyz
abcd axyz
! Abcd dcbA !
 Abcd dcbA !
Abcd dcbA

27)Golang TrimRight() [区分大小写]

TrimRight函数仅从字符串s的右侧删除预定义字符cutset。

语法

func TrimRight(s string, cutset string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.TrimRight("0120 2510", "0"))
    fmt.Println(strings.TrimRight("abcd axyz", "a"))
    fmt.Println(strings.TrimRight("abcd axyz", "A"))
    fmt.Println(strings.TrimRight("! Abcd dcbA !", "A"))
    fmt.Println(strings.TrimRight("! Abcd dcbA !", "!"))
    fmt.Println(strings.TrimRight(" Abcd dcbA ", " "))
}

输出结果

0120 251
abcd axyz
abcd axyz
! Abcd dcbA !
! Abcd dcbA
 Abcd dcbA

28)Golang TrimSpace()

TrimSpace函数从字符串的两侧删除空白和其他预定义字符。 “\t”-选项卡 “\n”-新行 “\x0B”-垂直选项卡 “\r”-回车 “”-普通空白

语法

func TrimSpace(s string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.TrimSpace(" I love my country "))
    fmt.Println(strings.TrimSpace(" \t\n  I love my country \t\n "))
    fmt.Println(strings.TrimSpace(" \t\n\r\x0BI love my country\t\n "))
}

输出结果

I love my country
I love my country
I love my country

29)Golang TrimPrefi() [区分大小写]

TrimPrefix函数从S字符串的开头删除前缀字符串。如果S不以前缀开头,则S将原封不动地返回。

语法

func TrimPrefix(S string, prefix string) string

DEMO

package main

import (
    "fmt"
    "strings"
)

func main() {
    var s string
    s = "I love my country"
    s = strings.TrimPrefix(s, "I")
    s = strings.TrimSpace(s)
    fmt.Println(s)

    s = "I love my country"
    s = strings.TrimPrefix(s, "i")
    fmt.Println(s)

    s = "\nI-love-my-country"
    s = strings.TrimPrefix(s, "\n")
    fmt.Println(s)

    s = "\tI-love-my-country"
    s = strings.TrimPrefix(s, "\t")
    fmt.Println(s)
}

输出结果

love my country
I love my country
I-love-my-country
I-love-my-country
Share