go语言的string提供的方法,涉及源码文件src/string/*
compare.go reader.go replace.go search.go strings.go
请读者自行查看源码相关内容
实例内容如下
package main
import (
"fmt"
"strings"
"strconv"
)
func main(){
var str string = "http://www.hello-hello.com "
cc := strings.HasPrefix(str, "http://")
fmt.Println(cc)
dd := strings.Index(str,"hel")
fmt.Println(dd)
ff := strings.LastIndex(str,"com")
fmt.Println("lastIndex com:",ff)
res := strings.Replace(str,"hello","ixe",2)
fmt.Println("replace:",res)
Cnt := strings.Count(res,"ixe")
fmt.Println("count:",Cnt)
res = strings.ToUpper(res)
fmt.Println("toUpper :", res)
res = strings.ToLower(res)
fmt.Println("toLower:",res)
res = strings.Replace(res," ","/",1)
fmt.Println("replace space:",res)
res = strings.TrimSpace(res)
fmt.Println("trimSpace:",res)
a := strconv.Itoa(100)
fmt.Println("Itoa:",a)
b,err := strconv.Atoi("100")
if err != nil {
fmt.Println("Atoi convert to int: ",err)
return
}
fmt.Println("Atoi convert:",b)
}
运行结果
~/gomod/src/strings$ go run string.go
true
11
lastIndex com: 23
replace: http://www.ixe-ixe.com
count: 2
toUpper : HTTP://WWW.IXE-IXE.COM
toLower: http://www.ixe-ixe.com
replace space: http://www.ixe-ixe.com/
trimSpace: http://www.ixe-ixe.com/
Itoa: 100
Atoi convert: 100