在 Go 语言中,`strings` 包提供了许多用于字符串操作的函数,这些函数非常高效且易于使用。以下是一些常用的 `strings` 包函数及其使用方法: --- ### **1. 字符串查询** - **`Contains(s, substr string) bool`** 判断字符串 `s` 是否包含子串 `substr`。 示例: ```go result := strings.Contains("Hello, World!", "World") fmt.Println(result) // 输出: true ``` - **`ContainsAny(s, chars string) bool`** 判断字符串 `s` 是否包含 `chars` 中的任意字符。 示例: ```go result := strings.ContainsAny("Hello, World!", "aeiou") fmt.Println(result) // 输出: true ``` - **`Count(s, substr string) int`** 统计子串 `substr` 在字符串 `s` 中出现的次数。 示例: ```go count := strings.Count("banana", "a") fmt.Println(count) // 输出: 3 ``` --- ### **2. 字符串前缀和后缀** - **`HasPrefix(s, prefix string) bool`** 判断字符串 `s` 是否以指定前缀开头。 示例: ```go result := strings.HasPrefix("Hello, World!", "Hello") fmt.Println(result) // 输出: true ``` - **`HasSuffix(s, suffix string) bool`** 判断字符串 `s` 是否以指定后缀结尾。 示例: ```go result := strings.HasSuffix("Hello, World!", "World!") fmt.Println(result) // 输出: true ``` --- ### **3. 字符串索引** - **`Index(s, substr string) int`** 返回子串 `substr` 在字符串 `s` 中首次出现的位置。如果未找到,返回 `-1`。 示例: ```go index := strings.Index("Hello, World!", "World") fmt.Println(index) // 输出: 7 ``` - **`LastIndex(s, substr string) int`** 返回子串 `substr` 在字符串 `s` 中最后一次出现的位置。如果未找到,返回 `-1`。 示例: ```go index := strings.LastIndex("Hello, World, World!", "World") fmt.Println(index) // 输出: 13 ``` --- ### **4. 字符串替换** - **`Replace(s, old, new string, n int) string`** 将字符串 `s` 中的 `old` 替换为 `new`,最多替换 `n` 次。如果 `n` 为 `-1`,则替换所有匹配项。 示例: ```go str := strings.Replace("banana", "a", "o", 2) fmt.Println(str) // 输出: bonona ``` - **`ReplaceAll(s, old, new string) string`** 替换字符串 `s` 中所有的 `old` 为 `new`。 示例: ```go str := strings.ReplaceAll("banana", "a", "o") fmt.Println(str) // 输出: bonono ``` --- ### **5. 字符串分割与拼接** - **`Split(s, sep string) []string`** 按照分隔符 `sep` 将字符串 `s` 分割成一个字符串切片。 示例: ```go parts := strings.Split("a,b,c", ",") fmt.Println(parts) // 输出: [a b c] ``` - **`Join(elems []string, sep string) string`** 将字符串切片 `elems` 使用分隔符 `sep` 拼接成一个字符串。 示例: ```go str := strings.Join([]string{"a", "b", "c"}, ",") fmt.Println(str) // 输出: a,b,c ``` - **`Fields(s string) []string`** 按空白字符(如空格、制表符等)将字符串 `s` 分割成一个字符串切片。 示例: ```go parts := strings.Fields("a b c") fmt.Println(parts) // 输出: [a b c] ``` --- ### **6. 字符串大小写转换** - **`ToLower(s string) string`** 将字符串 `s` 转换为小写形式。 示例: ```go str := strings.ToLower("HELLO") fmt.Println(str) // 输出: hello ``` - **`ToUpper(s string) string`** 将字符串 `s` 转换为大写形式。 示例: ```go str := strings.ToUpper("hello") fmt.Println(str) // 输出: HELLO ``` --- ### **7. 字符串修剪** - **`Trim(s, cutset string) string`** 移除字符串 `s` 开头和结尾的所有出现在 `cutset` 中的字符。 示例: ```go str := strings.Trim("!!!Hello, World!!!", "!") fmt.Println(str) // 输出: Hello, World ``` - **`TrimSpace(s string) string`** 移除字符串 `s` 开头和结尾的所有空白字符(包括空格、制表符、换行符等)。 示例: ```go str := strings.TrimSpace(" Hello, World! ") fmt.Println(str) // 输出: Hello, World! ``` --- ### **8. 字符串比较** - **`EqualFold(s1, s2 string) bool`** 比较两个字符串是否相等(忽略大小写)。 示例: ```go result := strings.EqualFold("GoLang", "golang") fmt.Println(result) // 输出: true ``` --- 以上是 `strings` 包中一些常用的函数及其使用方法。这些函数可以帮助开发者轻松处理各种字符串操作需求 .