• golang学习strings包


    string操作在编程中具有极高的频率,那么string中有哪些有用的方法呢?

    strings 包与 bytes 包中的函数用法基本一样,不再赘述。
    
    只对 Replacer 进行说明。
    
    ------------------------------------------------------------
    
    // 转换
    
    func ToUpper(s string) string
    func ToLower(s string) string
    func ToTitle(s string) string
    
    func ToUpperSpecial(_case unicode.SpecialCase, s string) string
    func ToLowerSpecial(_case unicode.SpecialCase, s string) string
    func ToTitleSpecial(_case unicode.SpecialCase, s string) string
    
    func Title(s string) string
    
    ------------------------------
    
    // 比较
    
    func Compare(a, b string) int
    
    func EqualFold(s, t string) bool
    
    ------------------------------
    
    // 清理
    
    func Trim(s string, cutset string) string
    func TrimLeft(s string, cutset string) string
    func TrimRight(s string, cutset string) string
    
    func TrimFunc(s string, f func(rune) bool) string
    func TrimLeftFunc(s string, f func(rune) bool) string
    func TrimRightFunc(s string, f func(rune) bool) string
    
    func TrimSpace(s string) string
    
    func TrimPrefix(s, prefix string) string
    func TrimSuffix(s, suffix string) string
    
    ------------------------------
    
    // 拆合
    
    func Split(s, sep string) []string
    func SplitN(s, sep string, n int) []string
    
    func SplitAfter(s, sep string) []string
    func SplitAfterN(s, sep string, n int) []string
    
    func Fields(s string) []string
    func FieldsFunc(s string, f func(rune) bool) []string
    
    func Join(a []string, sep string) string
    
    func Repeat(s string, count int) string
    
    ------------------------------
    
    // 子串
    
    func HasPrefix(s, prefix string) bool
    func HasSuffix(s, suffix string) bool
    
    func Contains(s, substr string) bool
    func ContainsRune(s string, r rune) bool
    func ContainsAny(s, chars string) bool
    
    func Index(s, sep string) int
    func IndexByte(s string, c byte) int
    func IndexRune(s string, r rune) int
    func IndexAny(s, chars string) int
    func IndexFunc(s string, f func(rune) bool) int
    
    func LastIndex(s, sep string) int
    func LastIndexByte(s string, c byte) int
    func LastIndexAny(s, chars string) int
    func LastIndexFunc(s string, f func(rune) bool) int
    
    func Count(s, sep string) int
    
    ------------------------------
    
    // 替换
    
    func Replace(s, old, new string, n int) string
    
    func Map(mapping func(rune) rune, s string) string
    
    ------------------------------------------------------------
    
    type Reader struct { ... }
    
    func NewReader(s string) *Reader
    
    func (r *Reader) Read(b []byte) (n int, err error)
    func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
    func (r *Reader) WriteTo(w io.Writer) (n int64, err error)
    func (r *Reader) Seek(offset int64, whence int) (int64, error)
    
    func (r *Reader) ReadByte() (byte, error)
    func (r *Reader) UnreadByte() error
    
    func (r *Reader) ReadRune() (ch rune, size int, err error)
    func (r *Reader) UnreadRune() error
    
    func (r *Reader) Len() int
    func (r *Reader) Size() int64
    func (r *Reader) Reset(s string)
    
    ------------------------------------------------------------
    
    type Replacer struct { ... }
    
    // 创建一个替换规则,参数为“查找内容”和“替换内容”的交替形式。
    // 替换操作会依次将第 1 个字符串替换为第 2 个字符串,将第 3 个字符串
    // 替换为第 4 个字符串,以此类推。
    // 替换规则可以同时被多个例程使用。
    func NewReplacer(oldnew ...string) *Replacer
    
    // 使用替换规则对 s 进行替换并返回结果。
    func (r *Replacer) Replace(s string) string
    
    // 使用替换规则对 s 进行替换并将结果写入 w。
    // 返回写入的字节数和遇到的错误。
    func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)


    ★ 题目:使用映射(map)实现WordCount
    https://tour.go-zh.org/moretypes/23
    练习:映射
    实现 WordCount。它应当返回一个映射,其中包含字符串 s 中每个“单词”的个数。函数 wc.Test 会对此函数执行一系列测试用例,并输出成功还是失败。

    你会发现 strings.Fields 很有帮助。

    package main
    
    import (
        "golang.org/x/tour/wc"
        "strings"
    )
    
    func WordCount(s string) map[string]int {
        words := strings.Fields(s)
        m := make(map[string]int)
        for _, word := range words {
            n, ok := m[word]
            if ok == false {
                m[word] = 1
            } else {
                m[word] = n + 1
            }
        }
        return m
    }
    
    func main() {
        wc.Test(WordCount)
    }
  • 相关阅读:
    友链
    利用jenkins插件查看allure报告
    python pyyaml操作yaml配置文件
    数组类型
    接口测试--加密算法
    python赋值,深拷贝和浅拷贝的区别
    RF中在测试用例集上设置标签
    python中json.dump()与json.dumps()的区别
    python 日期与字符串之间的转换
    python operator操作符函数
  • 原文地址:https://www.cnblogs.com/youxin/p/15996845.html
Copyright © 2020-2023  润新知