Golang strconv和strings 常用方法

package main

import (
	"fmt"
	"strings"
	"strconv"
)

func main() {
	//1).长度
	var str01 string = "hello"
	fmt.Println("str01长度: ",len(str01))			//5
	fmt.Println("str01长度: ",len("world 哈哈"))	//12  汉字3个字节

	//2).字符串遍历
	var str02 = "hello 中国"
	//有乱码--》h  e  l  l  o     ä  ¸  ­  å    ½
	for i := 0; i<len(str02); i++ {
		fmt.Printf("%c  ",str02[i])
	}

	fmt.Println()
	//无乱码方式一--》h  e  l  l  o     中  国
	str03 := []rune(str02)
	for i := 0; i<len(str03); i++ {
		fmt.Printf("%c  ",str03[i])
	}
	fmt.Println()
	//无乱码方式二--》0-h 1-e 2-l 3-l 4-o 5-  6-中 9-国
	for index,val := range(str02) {
		fmt.Printf("%d-%c ",index,val)
	}
	fmt.Println()
	//3)字符串转整形数字
	//方式一:strconv.Atoi
	n,err := strconv.Atoi("123")
	if err != nil {
		fmt.Println("字符串转整形-转换错误")
	} else {
		fmt.Printf("%T,%d",n,n)
	}
	fmt.Println()
	//方式二:strconv.ParseInt("123",10,64)
	num,err := strconv.ParseInt("123",10,64)
	if err != nil {
		fmt.Println("字符串转整形-转换错误")
	} else {
		fmt.Printf("%T,%d",num,num)
	}
	fmt.Println()
	//4)整形转字符串
	//方式一
	st := strconv.Itoa(12345)
	fmt.Printf("%T,%s",st,st)
	fmt.Println()
	//方式二
	st1 := strconv.FormatInt(int64(12345),10)
	fmt.Printf("%T,%s",st1,st1)
	fmt.Println()
	//5) 字符串转 []byte
	var bytes = []byte("abc")
	fmt.Printf("bytes: %v",bytes)
	fmt.Println()

	//6) []byte转字符串
	sst := string([]byte{97,98,99})
	fmt.Printf("string: %s",sst)
	fmt.Println()

	//7)字符串的包含
	var b1 = strings.Contains("123hello","hell")	//true
	var b2 = strings.Contains("123hello","hel2")	//false
	fmt.Println(b1)
	fmt.Println(b2)

	//8)统计字符的个数
	strNum := strings.Count("chinese","e")
	fmt.Println(strNum)

	//9) == 区分大小写  EqualFold不区分大小写
	fmt.Println("abc" == "ABC")		//false
	fmt.Println("abc" == "abc")		//true
	fmt.Println(strings.EqualFold("abc","ABc"))		//true

	//10)返回下标位置 如果没有返回 -1
	fmt.Println(strings.Index("china","in"))  //2

	//11)最后出现的位置 如果没有返回 -1
	fmt.Println(strings.LastIndex("china inc es","in"))  //6

	//12) 替换字符串 得到一个新的字符串,原本的没有变 -1代表全部替换
	fmt.Println(strings.Replace("come on come","come","qq",1))		//qq on come
	fmt.Println(strings.Replace("come on come","come","qq",2))		//qq on qq
	fmt.Println(strings.Replace("come on come","come","qq",-1))		//qq on qq

	//12) 分割字符串成字符串数组
	strArr := strings.Split("china,qq,kik",",")
	fmt.Println(strArr)
	//遍历
	for index,val := range(strArr) {
		fmt.Println(index," ",val)
	}

	//13) 字符串大小写转换
	fmt.Println(strings.ToLower("abc HeLlo"))
	fmt.Println(strings.ToUpper("ABc haha"))

	//14) 字符串去左右空格
	fmt.Println(strings.TrimSpace("  abc HeLlo  "))

	//15) 字符串去掉指定字符
	fmt.Printf("str1:%q\n",strings.Trim("!  abc china  ","!"))		//str1:"  abc china  "
	fmt.Printf("str2:%q\n",strings.Trim("!  abc china  "," !"))		//str2:"abc china"

	//16 去掉字符串左边或者右边
	fmt.Printf("str1:%q\n",strings.TrimLeft("!  abc china  !","!"))			//str1:"  abc china  !"
	fmt.Printf("str1:%q\n",strings.TrimRight("!  abc china  !"," !"))		//str1:"!  abc china"

	//17) 判断字符串前缀和后缀
	fmt.Printf("%t",strings.HasPrefix("http://192.168.179.10","http"))		//true
	fmt.Println()
	fmt.Printf("%t",strings.HasSuffix("ttyh.jpg",".jpg"))					//true

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值