Golang学习笔记(09-3-字符串操作)

75 阅读2分钟
### 1.3. 返回值为切片
  1. strings.Split() Func: Split(s, sep string) []string Man: 以sep切割字符串s

  2. strings.SplitN() Func: SplitN(s, sep string, n int) []string Man: 以sep切割字符串n次,n<0返回所有子字符串切片;n=0返回nil


---

## 2. strconv
strconv用户完成字符串与其它基本数据类型之间的转换,必须要掌握!
### 2.1. 其它基础数据类型转字符串
  1. strconv.FormatBool() Func: FormatBool(b bool) string Man: 根据b的值返回"true"或"false"

  2. strconv.FormatInt() Func: FormatInt(i int64, base int) string Man: 返回i的base进制的字符串表示。base 必须在2到36之间

  3. strconv.FormatUint() Func: FormatUint(i uint64, base int) string Man: FormatInt的无符号整数版本

  4. strconv.FormatFloat() Func: FormatFloat(f float64, fmt byte, prec, bitSize int) string Man: 函数将浮点数表示为字符串并返回; bitSize表示f的来源类型(32:float32、64:float64),会据此进行舍入 fmt表示格式:'f'(-ddd.dddd);prec控制精度(排除指数部分,小数位数)

  5. strconv.Itoa() Func: Itoa(i int) string Man: Itoa是FormatInt(i, 10) 的简写,将int转为字符串


### 2.2. 字符串转其它数据类型
  1. strconv.ParseBool() Func: ParseBool(str string) (value bool, err error) Man: 返回字符串表示的bool值。它接受1、0、t、f、T、F、true、false、True、False、TRUE、FALSE;否则返回错误

  2. strconv.ParseInt() Func: ParseInt(s string, base int, bitSize int) (i int64, err error) Man: 返回字符串表示的整数值,接受正负号,base指定进制(2到36), bitSize指定结果必须能无溢出赋值的整数类型,0、8、16、32、64 分别代表 int、int8、int16、int32、int64;

  3. strconv.ParseUint() Func: ParseUint(s string, base int, bitSize int) (n uint64, err error) Man: ParseUint类似ParseInt但不接受正负号,用于无符号整型

  4. strconv.ParseFloat() Func: ParseFloat(s string, bitSize int) (f float64, err error) Man: 解析一个表示浮点数的字符串并返回其值。 bitSize表示f的来源类型(32:float32、64:float64),会据此进行舍入

  5. strconv.Atoi() Func: Atoi(s string) (i int, err error) Man: IAtoi是ParseInt(s, 10, 0)的简写,将字符串转为int