亲宝软件园·资讯

展开

Golang strings包字符串操作

Linux猿 人气:0

在编写代码的时候最常用到的就是字符串了,Golang 中的字符串统一使用 UTF-8 (属于Unicode编码的一种实现方式)进行编码,本篇文章将结合具体实例对常用的字符串操作函数进行介绍。

func Contains

func Contains(s, substr string) bool

作用:判断 substr 是否是 s 的子串,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsContains("Linux", "in"))
  fmtPrintln(stringsContains("Linux", "Unix"))
  fmtPrintln(stringsContains("Linux", ""))
  fmtPrintln(stringsContains("", ""))
}

输出:
true
false
true
true

func HasPrefix

func HasPrefix(s, prefix string) bool

作用:字符串 s 是否以 prefix 为开头,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsHasPrefix("Linux", "Lin"))
  fmtPrintln(stringsHasPrefix("Linux", "in"))
  fmtPrintln(stringsHasPrefix("Linux", ""))
}

输出:
true
false
true

func HasSuffix

func HasSuffix(s, suffix string) bool

作用:判断字符串 s 是否以 suffix 结尾,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsHasSuffix("Linux", "nux"))
  fmtPrintln(stringsHasSuffix("Linux", "ix"))
  fmtPrintln(stringsHasSuffix("Linux", ""))
}

输出:
true
false
true

func Replace

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

作用:返回 s 中前 n 个不重复的 old 子串替换为 new 子串的新字符串,如果 n < 0 ,则替换所有 old 子串,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsReplace("Linux is very very very good!", "very", "much", 2))
  fmtPrintln(stringsReplace("Linux is very very very good!", "very", "much", -1))
}

输出:
Linux is much much very good!
Linux is much much much good!

func Split

func Split(s, sep string) []string

作用:返回将字符串 s 按 sep子串分割的字符串切片,sep 为空字符串时,将 s 分割为每一个 unicode 码值的字符串切片。例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsSplit("Linux, Unix, Windows, Android", ", "))
  fmtPrintln(stringsSplit(" Linux is very very very good! ", " "))
}

输出:返回的是字符串数组。
[Linux Unix Windows Android]
[ Linux is very very very good! ]

func ToLower

func ToLower(s string) string

作用:返回字符串 s 中字母转小写的拷贝,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsToLower("Linux, Unix, Windows, Android"))
  fmtPrintln(stringsToLower(" Linux is very very very good! "))
}

输出:
linux, unix, windows, android
 linux is very very very good!

func ToUpper

func ToUpper(s string) string

作用:返回字符串 s 中字母转大写的拷贝,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsToUpper("Linux, Unix, Windows, Android"))
  fmtPrintln(stringsToUpper(" Linux is very very very good! "))
}

输出:
LINUX, UNIX, WINDOWS, ANDROID
LINUX IS VERY VERY VERY GOOD! 

func Repeat

func Repeat(s string, count int) string

作用:返回 count 个字符串 s 相连接的字符串,如果 count 为负数 或(len * s * count)的结果溢出,则报 panic 异常。例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsRepeat("Linux", 6))
  fmtPrintln(stringsRepeat("Linux", 0))
  fmtPrintln(stringsRepeat("Linux", -1))
}

输出:
LinuxLinuxLinuxLinuxLinuxLinux
 
panic: strings: negative Repeat count
 
goroutine 1 [running]:
stringsRepeat(0x4bcf3d, 0x5, 0xffffffffffffffff, 0x1, 0x1)
        /usr/local/go/src/strings/stringsgo:529 +0x5e5
mainmain()
        /root/goProject/src/test/maingo:11 +0x167

func Count

func Count(s, substr string) int

作用:返回字符串 s 中包含的不重叠的子串 substr 个数,如果 substr 是一个空字符串,则返回1 + s中的Unicode代码点数(Unicode 代码点:可以简单理解为一个符号便是一个代码点),例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsCount("Golang Golang Golang", "Go"))
  fmtPrintln(stringsCount("Golang", ""))
  fmtPrintln(stringsCount("Golang语言", ""))
}

输出:
3
7
9

func Index

func Index(s, substr string) int

作用:返回字符串 s 中包含的第一个子串 substr 的索引,如果不存在返回 -1,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  fmtPrintln(stringsIndex("Golang", "lang"))
  fmtPrintln(stringsIndex("Golang", "Linux"))
}

输出:
2
-1

func Join

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

作用:使用 sep 作为分隔符,将elems 中的所有字符连接起来,例如:

package main
 
import (
  "fmt"
  "strings"
)
 
func main() {
  elems := []string{"I", "like", "golang", "!"}
  fmtPrintln(stringsJoin(elems, " "))
  elems = []string{"123", "456", "789"}
  fmtPrintln(stringsJoin(elems, "-"))
}

 输出:
[root@localhost gotest]# go run maingo
I like golang !
123-456-789
[root@localhost gotest]#

加载全部内容

相关教程
猜你喜欢
用户评论