> For the complete documentation index, see [llms.txt](https://yangsx95.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yangsx95.gitbook.io/notes/programming-language/golang/chang-yong-api/zi-fu-chuan-cao-zuo.md).

# 字符串操作

* 统计字符串长度

  ```go
  len(str) // 内置函数
  ```
* 字符串遍历

  ```go
  r := []rune(str)
  ```
* 字符串转换为整数

  ```go
  n, err = strconv.Atoi("66")
  ```
* 整数转换字符串

  ```go
  str = strconv.Itoa(6887)
  ```
* 查找子串是否在目标字符串中

  ```go
  strings.Contains("Hello World", "llo")
  ```
* 统计字符串中包含几个指定的子串

  ```go
  strings.Count("Hello World", "o")
  ```
* 判断字符是否以指定字符开始、结束

  ```go
  strings.HasPrefix("http://yangsx95.com", "http")
  strings.HasSuffix("http://yangsx95.com", ".com")
  ```
* 不区分大小写比较字符串

  ```go
  strings.EqualFold("go", "GO")
  ```
* 查找指定子串在字符串中第一次出现的索引位置

  ```go
  strings.IIndex("Hello World", "o")
  ```
* 字符串替换，n是替换的次数

  ```go
  strings.Replace("Hello World", "o", "hah", n)
  ```
* 字符串分割

  ```go
  strings.Split("Hello World", "o")
  ```
* 字母大小写转换

  ```go
  strings.ToLower("Go") // go
  strings.ToUpper("Go") // GO
  ```
* 剔除两侧指定字符

  ```go
  strings.Trim("~go~", "~") // go
  ```
* 剔除两侧空格字符

  ```go
  strings.TrimSpace("  go ") // go
  ```
* 剔除左侧指定字符

  ```go
  strings.TrimLeft("~go~", "~") // go~
  ```
* 剔除右侧指定字符

  ```go
  strings.TrimRight("~go~", "~") // ~go
  ```
