go string 应用方法汇总与实例

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值