GO语言学习之数据类型-->基本类型(字符串)

一、基本类型介绍

类型 默认值说明
bool   false  
byte   0  
rune   0  
int, uint   0  
int8, uint8   0  
int16, uint16   0  
int32, uint32   0  
int64, uint64   0  
float32   0.0  浮点型
float64   0.0  浮点型
complex64      复数
complex128      复数
uintptr     以存储指针的 uint32 或 uint64 整数
array     数组
struct     结构体
string   "" UTF-8 字符串
slice   nil 引用类型
map   nil 引用类型
channel   nil 引用类型
interface   nil 接口
function   nil 函数

 二、数据类型之字符串

字符串声明

s1 := "hello"
s2 := "你好"

多行字符串声明  

s1 := `
  第一行
  第二行
  第三行
`
#必须使用反引号`` 括起来

字符串的操作方法

方法 介绍
len(str) 求长度
+ 或  fmt.Sprintf 拼接字符串

string.Split

分割字符串
string.Contains 判断是否包含

string.HasPrefix;

string.HasSuffix

前缀/后缀判断

string.Index()

string.LastIndex()

字符串出现的位置
string.Join(a[]string,sep string) Join操作

三、常用字符串操作函数

判断字符串s,是否有前缀字符串prefix.

func HasPrefix(s,prefix string)  bool

fmt.Println(strings.HasPrefix("Gopher","Go")) //判断Gopher的前缀是否是Go

//true

判断字符串s,是否有后缀字符串suffix

func HasSuffix(s, suffix string) bool

fmt.Println(strings.HasSuffix("AmiGo","Go"))  //判断AmiGo的后缀是否是Go
//true

判断字符串s,是否包含子串substr

fucn Contains(s,substr string) bool

var str01 string  = "Hello world"
var isCon bool = strings.Contains(str01,"Hello")   //判断字符串str01里是否包含Hello
fmt.Println(isCon)
//true 

统计给定子串sep在字符串s中的出现次数,sep为空时,返回1+字符串的长度

func Count(s,sep string)  int

fmt.Println(strings.Count("chineseeee","ee"))   //出现两次,返回2
fmt.Println(strings.Count("chineseeee","ww"))  //出现0次,返回0
fmt.Println(strings.Count("five","")) //为空,返回1+字符串"five"的长度,返回5

返回字符串s中子串sep第一次出现的位置(索引)

func Index(s,sep string) int

fmt.Println(strings.Index("Hello,world","w")) //返回6
fmt.Println(strings.Index("Hello,world","q"))  //不存在,返回-1

返回字符串s中子串sep最后一次出现的位置(索引)

func LastIndex(s,sep string) int

fmt.Println(strings.LastIndex("Hello,world","o")) //返回7

返回字符串s转小写的Copy

func ToLower(s string)  string

fmt.Println(strings.ToLower("Hello Hao Hao Hao")) 

//全变小写,返回 hello hao hao hao

返回字符串s转大写的Copy

func ToUpper(s string) string

fmt.Println(strings.ToUpper("hello hao hao hao"))
//全部变大写, 返回HELLO HAO HAO HAO

重复s字符串count次,最后返回新生成的字符串

fmt.Println("hello," + strings.Repeat("world",2))
//先打印hello,然后打印2次world,因为world重复了2次
//所以返回hello,worldworld

在s字符串中,把old字符串替换成new字符串,n表示替换的次数,小于0 表示全部替换

func Replace(s,old, new string , n int)

var str03 string = "/Users/Documents/GOPatch/src/MyGO/config/TestString/"
fmt.Println(strings.Replace(str03,"/","*" , -1))
fmt.Println(strings.Replace(str03,"/","*",4))
//第一个是-1,小于0,所以会全部把“/”替换为“*”,返回:*Users*Documents*GOPatch*src*MyGO*config*TestString
//第二个是4,所以会把前4个“/”替换为“*”,返回:*Users*Documents*GOPatch*src/MyGO/config/TestString/

返回去掉字符串s前后端(两边结尾)所有cutset子串的字符串

func Trim(s string,cutset string)  string

var str03 string = "/Users/Documents/GOPatch/src/MyGO/config/TestString/"
fmt.Println(strings.Trim(str03,"/"))
#去掉两边的"/",返回:Users/Documents/GOPatch/src/MyGO/config/TestString

返回去掉字符串s前端(左边开头)cutset子串的字符串

func TrimLeft(s string,cutset string) string

var str03 string = "/Users/Documents/GOPatch/src/MyGO/config/TestString/"
fmt.Println(strings.TrimLeft(str03,"/"))
#去掉左边的"/",返回:Users/Documents/GOPatch/src/MyGO/config/TestString/

返回去掉字符串后端(右边开头)cutset子串的字符串

func TrimRight(s string,cutset string) string

var str03 string = "/Users/Documents/GOPatch/src/MyGO/config/TestString/"
fmt.Println(strings.TrimRight(str03,"/"))
#去掉右边的"/",返回:/Users/Documents/GOPatch/src/MyGO/config/TestString

删除字符串s前后端空格

func TrimSpace(s string) string

fmt.Println(strings.TrimSpace(" Hello,world "))
//删除左右两边空格
//返回:Hello,world

 将一个字符串s按空格进行分割

func Fields(s string)

func main()  {
    Str := "  hello   it's  a  nice day today    "
    fieldsSlece := strings.Fields(Str)
    fmt.Println(fieldsSlece)

    for i, v := range fieldsSlece {         
        fmt.Printf("下标 %d 对应值 = %s 
",  i, v)
    }

}

结果:
[hello it's a nice day today]  #按空格将字符串分割成 字符串数组(字符串切片)了

下标 0 对应值 = hello 下标 1 对应值 = it's 下标 2 对应值 = a 下标 3 对应值 = nice 下标 4 对应值 = day 下标 5 对应值 = today

将字符串按特定字符分割

func Split(s,sep string)

slice01 := strings.Split("h-e-l-l-o-w","-")
    fmt.Println(slice01)

    for i,v := range slice01 {
        fmt.Printf("下标 %d 对应值 = %s 
", i, v)
    }

结果:

[h e l l o w]  //按”-“字符分割的结果,转换成字符串数组了(也叫字符串切片),字符串数组(字符串切片)才能遍历.

//这个是遍历字符串切片,打印出下标和对应的值
下标 0 对应值 = h 
下标 1 对应值 = e 
下标 2 对应值 = l 
下标 3 对应值 = l 
下标 4 对应值 = o 
下标 5 对应值 = w 

将字符串类型为string的slice,使用分割符号拼接成一个字符串

func Join(a []string,sep string)

str01 := "hello   it's  a  nice day today"
fieldsSlece := strings.Fields(str01)  //将字符串分割成字符串切片,也叫[]string
fmt.Println(fieldsSlece) //打印字符串切片 fmt.Println(strings.Join(fieldsSlece,",")) ,将字符串切片以","拼接成字符串 结果: [hello it's a nice day today] //这是字符串切片 hello,it's,a,nice,day,today //这是字符串切片用","拼接成的字符串
原文地址:https://www.cnblogs.com/chadiandianwenrou/p/13738136.html