当前位置:网站首页>字符串操作

字符串操作

2022-07-07 05:27:00 Jimmy_jimi

package main

import (
	"fmt"
	"strconv"
	"strings"

	//"go/types"
)




func main()  {
    
	str := "hellow元素"
	//统计长度
	fmt.Println(len(str))
	//字符串遍历
	r := []rune(str)
	for i:=0;i<len(r);i++{
    
		fmt.Printf("%c ",r[i])
	}
	//字符串转整数
	n,_ := strconv.Atoi("12")
	fmt.Printf("\ntype:%T,date:%d",n,n)
	//整形转字符串
	//%s 字符串,%v内置格式内容
	//%v 输出所有值,%+v输出字段类型和值,%#V 输出结构体名字和结构体
	m := strconv.Itoa(1234)
	fmt.Printf("\ntype:%T,date:%v",m,m)
	//字符串转[]byte
	//byte 是uint8的别名,表示只占一个字节的ASCII码
	println(" ")
	var bytes = []byte("hellow go")
	fmt.Printf("%v",bytes)
	//判断是否存在字串
	fmt.Printf("\n是否存在字串:%v",strings.Contains("safes","sa"))
	//统计有几个字串
	fmt.Printf("\n%v个字串",strings.Count("safeeeees","e"))
	//字符转比较
	// "=="区分大小写,EqualFold不区分
	fmt.Printf("\n%v",strings.EqualFold("abc","AbC"))
	fmt.Println("abc"=="aBc")
	//返回字串第一次和最后一次出现的位置,没有返回-1
	fmt.Printf("%v",strings.Index("aaaaaa","a"))
	fmt.Printf("\n%v",strings.LastIndex("aaaaaa","a"))
	//替换,n指像替换的数量,-1为全部
	fmt.Printf("\n%v",strings.Replace("go to learn go","go","c 语言",-1))
	//拆分,结果为字符转数组
	fmt.Printf("\n%v",strings.Split("go to learn go" ," "))
	//判断是否以字串开始/结束
	fmt.Printf("\n%v",strings.HasPrefix("sdqwe.log","s"))
	fmt.Printf("\n%v",strings.HasSuffix("sdqwe.log","log"))

}
原网站

版权声明
本文为[Jimmy_jimi]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42269472/article/details/125644894