Go 语言学习 —— strings 细说

Go 语言 strings 包与 C++、Python 字符串处理对比

在现代编程中,字符串处理是一个基础而重要的任务。不同编程语言提供了各自的字符串处理函数和方法,今天我们将详细介绍 Go 语言的 strings 包,并对比 C++ 和 Python 中的字符串处理方法。通过这篇文章,你将能够更好地理解这些语言在字符串处理上的异同,从而选择最适合你项目需求的工具。

Go 语言的 strings

Go 语言的 strings 包提供了丰富的字符串操作函数,这些函数设计简单明了,非常适合进行常见的字符串处理任务。以下是 Go 语言 strings 包中的一些常用函数及其说明:

  1. strings.Contains(s, substr string) bool

    检查字符串 s 是否包含子字符串 substr

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    s := "hello world"
    substr := "world"
    fmt.Println(strings.Contains(s, substr)) // 输出: true
    }
  2. strings.HasPrefix(s, prefix string) bool

    检查字符串 s 是否以 prefix 开头。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    s := "hello world"
    prefix := "hello"
    fmt.Println(strings.HasPrefix(s, prefix)) // 输出: true
    }
  3. strings.HasSuffix(s, suffix string) bool

    检查字符串 s 是否以 suffix 结尾。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    s := "hello world"
    suffix := "world"
    fmt.Println(strings.HasSuffix(s, suffix)) // 输出: true
    }
  4. strings.Index(s, substr string) int

    返回子字符串 substr 在字符串 s 中第一次出现的位置。如果未找到,则返回 -1

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    s := "hello world"
    substr := "world"
    fmt.Println(strings.Index(s, substr)) // 输出: 6
    }
  5. strings.LastIndex(s, substr string) int

    返回子字符串 substr 在字符串 s 中最后一次出现的位置。如果未找到,则返回 -1

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    s := "hello world world"
    substr := "world"
    fmt.Println(strings.LastIndex(s, substr)) // 输出: 12
    }
  6. strings.Split(s, sep string) []string

    按照分隔符 sep 拆分字符串 s,返回一个字符串切片。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    s := "a,b,c"
    sep := ","
    result := strings.Split(s, sep)
    fmt.Println(result) // 输出: [a b c]
    }
  7. strings.Join(a []string, sep string) string

    将字符串切片 a 用分隔符 sep 连接成一个字符串。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    a := []string{"a", "b", "c"}
    sep := ","
    result := strings.Join(a, sep)
    fmt.Println(result) // 输出: a,b,c
    }
  8. strings.ToLower(s string) string

    将字符串 s 转换为小写字母。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    s := "Hello World"
    result := strings.ToLower(s)
    fmt.Println(result) // 输出: hello world
    }
  9. strings.ToUpper(s string) string

    将字符串 s 转换为大写字母。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    s := "Hello World"
    result := strings.ToUpper(s)
    fmt.Println(result) // 输出: HELLO WORLD
    }
  10. strings.Trim(s, cutset string) string

    移除字符串 s 两端的所有字符,如果这些字符出现在 cutset 中。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    package main

    import (
    "fmt"
    "strings"
    )

    func main() {
    s := "!!!hello!!!"
    cutset := "!"
    result := strings.Trim(s, cutset)
    fmt.Println(result) // 输出: hello
    }

C++ 的字符串处理函数

C++ 中的字符串处理主要通过 std::string 类提供。std::string 提供了许多成员函数来操作和处理字符串:

  1. std::string::find(const std::string& substr) const

    查找子字符串 substr 在当前字符串中的第一次出现位置。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include <iostream>
    #include <string>

    int main() {
    std::string s = "hello world";
    std::string substr = "world";
    std::cout << s.find(substr) << std::endl; // 输出: 6
    return 0;
    }
  2. std::string::rfind(const std::string& substr) const

    查找子字符串 substr 在当前字符串中的最后一次出现位置。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include <iostream>
    #include <string>

    int main() {
    std::string s = "hello world world";
    std::string substr = "world";
    std::cout << s.rfind(substr) << std::endl; // 输出: 12
    return 0;
    }
  3. std::string::substr(size_t pos = 0, size_t len = npos) const

    提取子字符串,从位置 pos 开始,长度为 len

    1
    2
    3
    4
    5
    6
    7
    8
    #include <iostream>
    #include <string>

    int main() {
    std::string s = "hello world";
    std::cout << s.substr(6, 5) << std::endl; // 输出: world
    return 0;
    }
  4. std::string::find_first_of(const std::string& chars) const

    查找在 chars 中的任意字符在当前字符串中的第一次出现位置。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include <iostream>
    #include <string>

    int main() {
    std::string s = "hello world";
    std::string chars = "o";
    std::cout << s.find_first_of(chars) << std::endl; // 输出: 4
    return 0;
    }
  5. std::string::find_last_of(const std::string& chars) const

    查找在 chars 中的任意字符在当前字符串中的最后一次出现位置。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include <iostream>
    #include <string>

    int main() {
    std::string s = "hello world";
    std::string chars = "o";
    std::cout << s.find_last_of(chars) << std::endl; // 输出: 7
    return 0;
    }
  6. std::transform(begin, end, begin, [](unsigned char c){ return std::tolower(c); })

    将字符串转换为小写字母。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <cctype>

    int main() {
    std::string s = "Hello World";
    std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
    std::cout << s << std::endl; // 输出: hello world
    return 0;
    }
  7. std::transform(begin, end, begin, [](unsigned char c){ return std::toupper(c); })

    将字符串转换为大写字母。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <cctype>

    int main() {
    std::string s = "Hello World";
    std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
    std::cout << s << std::endl; // 输出: HELLO WORLD
    return 0;
    }

Python 的字符串处理方法

Python 提供了非常丰富的字符串处理方法,操作字符串非常便捷:

  1. str.find(sub)

    查找子字符串 sub 在当前字符串中的第一次出现位置。如果未找到,则返回 -1

    1
    2
    3
    s = "hello world"
    substr = "world"
    print(s.find(substr)) # 输出: 6
  2. str.rfind(sub)

    查找子字符串 sub 在当前字符串中的最后一次出现位置。如果未找到,则返回 -1

    1
    2
    3
    s = "hello world world"
    substr = "world"
    print(s.rfind(substr)) # 输出: 12
  3. str.startswith(prefix)

    检查字符串是否以 prefix 开头。

    1
    2
    3
    s = "hello world"
    prefix = "hello"
    print(s.startswith(prefix)) # 输出: True
  4. str.endswith(suffix)

    检查字符串是否以 suffix 结尾。

    1
    2
    3
    s = "hello world"
    suffix = "world"
    print(s.endswith(suffix)) # 输出: True
  5. str.split(sep=None)

    按照分隔符 sep 拆分字符串,返回一个列表。

    1
    2
    3
    s = "a,b,c"
    sep = ","
    print(s.split(sep)) # 输出: ['a', 'b', 'c']
  6. str.join(iterable)

    将可迭代对象中的字符串用当前字符串连接。

    1
    2
    3
    a = ["a", "b", "c"]
    sep = ","
    print(sep.join(a)) # 输出: a,b,c
  7. str.lower()

    将字符串转换为小写字母。

    1
    2
    s = "Hello World"
    print(s.lower()) # 输出: hello world
  8. str.upper()

    将字符串转换为大写字母。

    1
    2
    s = "Hello World"
    print(s.upper()) # 输出: HELLO WORLD
  9. str.strip(chars=None)

    移除字符串两端的指定字符(如果 chars 被提供)。

    1
    2
    3
    s = "!!!hello!!!"
    chars = "!"
    print(s.strip(chars)) # 输出: hello

总结

通过对比 Go 语言的 strings 包、C++ 的 std::string 类和 Python 的字符串处理方法,可以看到不同语言在字符串处理上的特点:

  • Go 语言strings 包提供了简单且功能强大的字符串操作函数,非常适合进行基础的字符串处理。
  • C++ 提供了底层的字符串操作功能,允许通过标准库和算法库进行灵活的字符串处理,并可以进行高效的性能优化。
  • Python 的字符串处理方法丰富且易于使用,提供了许多便利的功能,使得字符串操作变得非常简单直观。

Go 语言学习 —— strings 细说
https://github.com/zhililab/2024/08/07/Learning-Golang-001/
作者
Zhi Li
发布于
2024年8月7日
许可协议