go:strings与strconv包

一、strings包

  1.strings.HasPrefix()

    strings.HasPrefix(s string, "prefix" string) bool 判断字符串s是否是以‘prefix’开头,返回值为bool类型

  2.strings.HasSuffix()

    strings.HasSuffix(s string,"prefix" string) bool 判断字符串s是否是以‘prefix’结尾,返回值为bool类型

  3.strings.Index()

    strings.Index(s string 'prefix' string) int 判断prefix在s中的位置,返回值int,没有返回-1

  4.strings.LastIndex()

    strings.LastIndex(s string,'prefix' string) int  判断prefix最后在s中出现的位置,返回值int,没有返回-1

  5.strings.Replace()

    strings.Replace(str string,old string,new string, n int) 字符串替换

  6.strings.Count()

    strings.Count(str string,sub strings) int 字符串计数

  7.strings.Repeat()

    strings.Repeat(str string,count int) string 重复count次str

  8.strings.ToLower()

    strings.ToLower(str string) string 字符串小写

  9.strings.ToUpper()

    strings.ToUpper(str string) string 字符串大写

 

  10.strings.TrimSpace()

    -strings.TrimSpace(str string) 去掉str首尾空格

    -strings.Trim(str string,cut string) 去掉字符串首尾cut字符

    - strings.TrimLeft(str string,cut string) 去掉字符串首cut字符

    -strings.TrimRight(str string,cut string) 去掉字符串尾cut字符

	s := " hello world hello "
	ret1 := strings.TrimSpace(s)            //hello world hello
	ret2 := strings.Trim(s, " hello ")      //world
	ret3 := strings.TrimLeft(s, " hello ")  //world hello 
	ret4 := strings.TrimRight(s, " hello ") // hello world
	fmt.Println(ret1, ret2, ret3, ret4)

  

  11.strings.Fields

    -strings.Fields(str string)返回str空格分隔的所有子串的slice

    -strings.Split(str string,split string) 返回str 字符串split分隔的所有子串的slice

    -strings.Join(s1 [] strings. sep strings) s1中元素用sep连接起来

func main() {
	s := " hello world "
	str := strings.Fields(s)
	fmt.Println(str) //[hello world]
	ret := strings.Split(s, "l")
	fmt.Println(ret) //[ he  o wor d ]
	str2 := strings.Join(ret, "l")
	fmt.Println(str2) // hello world
}

二、strconv包

  1.strconv.Itoa()

    strconv.Itoa(i int) string将一个整数转化为字符串

  2.strconv.Atoi()

    strconv.Atoi(str string) int,error 将整数转为字符串

	str := strconv.Itoa(1000)
	fmt.Println(str) // "1000"
	str2, error := strconv.Atoi("1000")
	if error == nil {
		fmt.Println(str2) // 1000
	}

  

你可能感兴趣的:(go:strings与strconv包)