Go字符串

1. 字符串的声明是使用

package main

import "fmt"

func main() {
	/*
	Go中的字符串是一个字节的切片。
		可以通过将其内容封装在“”中来创建字符串。Go中的字符串是Unicode兼容的,并且是UTF-8编码的。

	字符串是一些字节的集合。
		理解为一个字符的序列。
		每个字符都有固定的位置(索引,下标,index:从0开始,到长度减1)

	语法:"",``

	字符:--->对应编码表中的编码值
		A-->65
		B-->66
		a-->97
		...

	字节:byte-->uint8

	 */
	//1.定义字符串
	s1 := "hello中国"
	s2 := `hello world`
	fmt.Println(s1)
	fmt.Println(s2)

	//2.字符串的长度:返回的是字节的个数
	fmt.Println(len(s1))
	fmt.Println(len(s2))

	//3.获取某个字节
	fmt.Println(s2[0]) //获取字符串中的第一个字节
	a := 'h'
	b := 104
	fmt.Printf("%c,%c,%c
", s2[0], a, b) //h,h,h

	//4.字符串的遍历
	//for循环
	for i := 0; i < len(s2); i++ {
		//fmt.Println(s2[i])
		fmt.Printf("%c	", s2[i])
		//h	e	l	l	o	 	w	o	r	l	d	
	}
	fmt.Println()

	//for range
	for _, v := range s2 {
		//fmt.Println(i,v)
		fmt.Printf("%c", v) //hello world
	}
	fmt.Println()

	//5.字符串是字节的集合
	slice1 := []byte{65, 66, 67, 68, 69}
	s3 := string(slice1) //根据一个字节切片,构建字符串
	fmt.Println(s3) //ABCDE

	s4 := "abcdef"
	slice2 := []byte(s4) //根据字符串,获取对应的字节切片
	fmt.Println(slice2) //[97 98 99 100 101 102]

	//6.字符串不能修改
	fmt.Println(s4) //abcdef
	//s4[2] = 'B'
}
package main

import (
	"fmt"
)

func main() {

	//string底层是一个byte数组,因此string也可以进行切片处理
	str := "hellooooooo" // :=类型推导
	//使用切片获取到
	slice := str[6:]
	fmt.Println("slice=", slice) //slice= ooooo
	fmt.Printf("%p
", &str) //0xc0000401f0
	fmt.Printf("%p
", &slice) //0xc000040200

	//string是不可变的,也就说不能通过 str[0] = 'z' 方式来修改字符串
	//str[0] = 'z' [编译不会通过,报错,原因是string是不可变]

	//如果需要修改字符串,可以先将string -> []byte / 或者 []rune -> 修改 -> 重写转成string
	arr1 := []byte(str)
	arr1[0] = 'z'
	str = string(arr1)
	fmt.Println("str=", str) //str= zellooooooo
	//str的内存地址没有改变
	fmt.Printf("%p
", &str) //0xc0000401f0


	//我们转成[]byte后,可以处理英文和数字,但是不能处理中文
	//原因是 []byte 字节来处理 ,而一个汉字,是3个字节,因此就会出现乱码
	//解决方法是 将string 转成 []rune 即可, 因为 []rune是按字符处理,兼容汉字
	//[]byte-->uint8
	//[]rune-->int32
	arr2 := []rune(str)
	arr2[0] = '北'
	str2 := string(arr2)
	fmt.Println("str=", str2) //str= 北ellooooooo
}

2. strings包下的关于字符串的函数

package main

import (
	"fmt"
	"strings"
)

func main() {
	/*
	strings包下的关于字符串的函数

	 */
	s1 := "helloworld"
	//1.是否包含指定的内容-->bool
	fmt.Println(strings.Contains(s1, "abc")) //false
	//2.是否包含chars中任意的一个字符即可
	fmt.Println(strings.ContainsAny(s1, "abcd")) //true
	//3.统计substr在s中出现的次数
	fmt.Println(strings.Count(s1, "lloo")) //0

	//4.以xxx前缀开头,以xxx后缀结尾
	s2 := "20190525课堂笔记.txt"
	if strings.HasPrefix(s2, "201905") {
		fmt.Println("19年5月的文件。。")
	}
	if strings.HasSuffix(s2, ".txt") {
		fmt.Println("文本文档。。")
	}

	//索引
	//helloworld
	fmt.Println(strings.Index(s1, "lloo"))     //查找substr在s中的位置,如果不存在就返回-1
	fmt.Println(strings.IndexAny(s1, "abcdh")) //查找chars中任意的一个字符,出现在s中的位置
	fmt.Println(strings.LastIndex(s1, "l"))    //查找substr在s中最后一次出现的位置

	//字符串的拼接
	ss1 := []string{"abc", "world", "hello", "ruby"}
	s3 := strings.Join(ss1, "-")
	fmt.Println(s3) //abc-world-hello-ruby

	//切割
	s4 := "123,4563,aaa,49595,45"
	ss2 := strings.Split(s4, ",")
	//fmt.Println(ss2)
	for i := 0; i < len(ss2); i++ {
		fmt.Println(ss2[i])
	}

	//重复,自己拼接自己count次
	s5 := strings.Repeat("hello", 5)
	fmt.Println(s5) //hellohellohellohellohello

	//替换
	//helloworld
	s6 := strings.Replace(s1, "l", "*", -1)
	fmt.Println(s6) //he**owor*d
	//fmt.Println(strings.Repeat("hello",5))
	
	//大小写
	s7 := "heLLo WOrlD**123.."
	fmt.Println(strings.ToLower(s7)) //hello world**123..
	fmt.Println(strings.ToUpper(s7)) //HELLO WORLD**123..

	/*
	截取子串:
	substring(start,end)-->substr
	str[start:end]-->substr
		包含start,不包含end下标
	 */
	fmt.Println(s1) //helloworld
	s8 := s1[:5]
	fmt.Println(s8) //hello
	fmt.Println(s1[5:]) //world
}

3. strconv包

package main

import (
	"fmt"
	"strconv"
)

func main() {
	/*
	strconv包:字符串和基本类型之前的转换
		string convert
	 */
	//fmt.Println("aa"+100) //java中可以这样写
	//1.字符串转bool类型
	s1 := "true"
	b1, err := strconv.ParseBool(s1)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%T,%t
", b1, b1) //bool,true

	ss1 := strconv.FormatBool(b1)
	fmt.Printf("%T,%s
", ss1, ss1) //string,true

	//2.字符串转整数
	s2 := "100"
	i2, err := strconv.ParseInt(s2, 2, 64)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("%T,%d
", i2, i2) //int64,4

	ss2 := strconv.FormatInt(i2, 10)
	fmt.Printf("%T,%s
", ss2, ss2) //string,4

	//itoa() int-->string
	//atoi() string-->int
	i3, err := strconv.Atoi("-42") //转为int类型
	fmt.Printf("%T,%d
", i3, i3) //int,-42
	ss3 := strconv.Itoa(-42)
	fmt.Printf("%T,%s
", ss3, ss3) //string,-42
}
原文地址:https://www.cnblogs.com/yzg-14/p/12247492.html