4.14 go 字符串处理

package main

import (
	"fmt"
	"strings"
	"strconv"
)
func main(){
	/** 字符串操作  **/
	//1Contains 是否包含
	fmt.Println(strings.Contains("hello","hell"))
	//2Joins 组合
	s:=[]string{"as","df","sfd"}
	buf:=strings.Join(s,"*")
	fmt.Println("buf=",buf)
	//3Index 查看索引  不存在则返回-1
	fmt.Println(strings.Index("01234","33"))
	//4Repeat 重复复制
	buf2:=strings.Repeat("hahah",4)
	fmt.Println(buf2)
	//5Split指定分隔符拆分
	buf="1,e,3,2,d,4"
	arr:=strings.Split(buf,",")
	fmt.Println(arr)
	//6去掉两头空格 Trim  返回字符串
	fmt.Printf("*%s*",strings.Trim("      are  you ok     "," "))
	//7Fields 去掉所有空格  返回切片
	fmt.Println(strings.Fields("      are  you    ok     "))
	
	/****字符串转换****/
	//1 Append 系列函数将整型转换为字符串后,添加到现有的字节数组中。
	str:=make([]byte,0,100)
	str=strconv.AppendInt(str,3444,10)//以10进制整数添加
	str=strconv.AppendBool(str,true)
	str=strconv.AppendQuote(str,"abcds")
	str=strconv.AppendQuoteRune(str,'z')
	fmt.Println(string(str))//3444true"abcds"'z'
	
	
	
	
	//2 Format 系列函数把其他类型转换为字符串
	a:=strconv.FormatBool(false)
	b:=strconv.FormatInt(1234,10)
	d:=strconv.FormatUint(1234,10)
	c:=strconv.Itoa(1023)//整型转字符串 常用
	e:=strconv.FormatFloat(3.14,'f',-1,64)// 'f' 小数形式,-1 紧缩形式,可以查看帮助文档
	fmt.Println(a,b,c,d,e)
	
	//3字符串转变为其他类型
	flag,err:=strconv.ParseBool("true")
	if err==nil{
		fmt.Println(flag)
	}
	in,_:=strconv.Atoi("3456")//转换Wie整型常用
	fmt.Println(in)
	
	
	
	
}
发布了124 篇原创文章 · 获赞 94 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/h4241778/article/details/105347501
今日推荐