// Count 计算字符串 sep 在 s 中的非重叠个数
// 如果 sep 为空字符串,则返回 s 中的字符(非字节)个数 + 1
// 使用 Rabin-Karp 算法实现

[html] view plaincopy
  1. func Count(s, sep string) int
  2. func main() {
  3. s := "Hello,世界!!!!!"
  4. n := strings.Count(s, "!")
  5. fmt.Println(n) // 5
  6. n = strings.Count(s, "!!")
  7. fmt.Println(n) // 2
  8. }

------------------------------------------------------------

// Contains 判断字符串 s 中是否包含子串 substr
// 如果 substr 为空,则返回 true
func Contains(s, substr string) bool

[html] view plaincopy
  1. func main() {
  2. s := "Hello,世界!!!!!"
  3. b := strings.Contains(s, "!!")
  4. fmt.Println(b) // true
  5. b = strings.Contains(s, "!?")
  6. fmt.Println(b) // false
  7. b = strings.Contains(s, "")
  8. fmt.Println(b) // true
  9. }

------------------------------------------------------------

// ContainsAny 判断字符串 s 中是否包含 chars 中的任何一个字符
// 如果 chars 为空,则返回 false
func ContainsAny(s, chars string) bool

[html] view plaincopy
  1. func main() {
  2. s := "Hello,世界!"
  3. b := strings.ContainsAny(s, "abc")
  4. fmt.Println(b) // false
  5. b = strings.ContainsAny(s, "def")
  6. fmt.Println(b) // true
  7. b = strings.Contains(s, "")
  8. fmt.Println(b) // true
  9. }

------------------------------------------------------------

// ContainsRune 判断字符串 s 中是否包含字符 r
func ContainsRune(s string, r rune) bool

[html] view plaincopy
  1. func main() {
  2. s := "Hello,世界!"
  3. b := strings.ContainsRune(s, '\n')
  4. fmt.Println(b) // false
  5. b = strings.ContainsRune(s, '界')
  6. fmt.Println(b) // true
  7. b = strings.ContainsRune(s, 0)
  8. fmt.Println(b) // false
  9. }

------------------------------------------------------------

// Index 返回子串 sep 在字符串 s 中第一次出现的位置
// 如果找不到,则返回 -1,如果 sep 为空,则返回 0。
// 使用 Rabin-Karp 算法实现
func Index(s, sep string) int

[html] view plaincopy
  1. func main() {
  2. s := "Hello,世界!"
  3. i := strings.Index(s, "h")
  4. fmt.Println(i) // -1
  5. i = strings.Index(s, "!")
  6. fmt.Println(i) // 12
  7. i = strings.Index(s, "")
  8. fmt.Println(i) // 0
  9. }

------------------------------------------------------------

// LastIndex 返回子串 sep 在字符串 s 中最后一次出现的位置
// 如果找不到,则返回 -1,如果 sep 为空,则返回字符串的长度
// 使用朴素字符串比较算法实现
func LastIndex(s, sep string) int

[html] view plaincopy
  1. func main() {
  2. s := "Hello,世界! Hello!"
  3. i := strings.LastIndex(s, "h")
  4. fmt.Println(i) // -1
  5. i = strings.LastIndex(s, "H")
  6. fmt.Println(i) // 14
  7. i = strings.LastIndex(s, "")
  8. fmt.Println(i) // 20
  9. }

------------------------------------------------------------

// IndexRune 返回字符 r 在字符串 s 中第一次出现的位置
// 如果找不到,则返回 -1
func IndexRune(s string, r rune) int

[html] view plaincopy
  1. func main() {
  2. s := "Hello,世界! Hello!"
  3. i := strings.IndexRune(s, '\n')
  4. fmt.Println(i) // -1
  5. i = strings.IndexRune(s, '界')
  6. fmt.Println(i) // 9
  7. i = strings.IndexRune(s, 0)
  8. fmt.Println(i) // -1
  9. }

------------------------------------------------------------

// IndexAny 返回字符串 chars 中的任何一个字符在字符串 s 中第一次出现的位置
// 如果找不到,则返回 -1,如果 chars 为空,则返回 -1
func IndexAny(s, chars string) int

[html] view plaincopy
  1. func main() {
  2. s := "Hello,世界! Hello!"
  3. i := strings.IndexAny(s, "abc")
  4. fmt.Println(i) // -1
  5. i = strings.IndexAny(s, "dof")
  6. fmt.Println(i) // 1
  7. i = strings.IndexAny(s, "")
  8. fmt.Println(i) // -1
  9. }

------------------------------------------------------------

// LastIndexAny 返回字符串 chars 中的任何一个字符在字符串 s 中最后一次出现的位置
// 如果找不到,则返回 -1,如果 chars 为空,也返回 -1
func LastIndexAny(s, chars string) int

[html] view plaincopy
  1. func main() {
  2. s := "Hello,世界! Hello!"
  3. i := strings.LastIndexAny(s, "abc")
  4. fmt.Println(i) // -1
  5. i = strings.LastIndexAny(s, "def")
  6. fmt.Println(i) // 15
  7. i = strings.LastIndexAny(s, "")
  8. fmt.Println(i) // -1
  9. }

------------------------------------------------------------

// SplitN 以 sep 为分隔符,将 s 切分成多个子串,结果中不包含 sep 本身
// 如果 sep 为空,则将 s 切分成 Unicode 字符列表。
// 如果 s 中没有 sep 子串,则将整个 s 作为 []string 的第一个元素返回
// 参数 n 表示最多切分出几个子串,超出的部分将不再切分。
// 如果 n 为 0,则返回 nil,如果 n 小于 0,则不限制切分个数,全部切分
func SplitN(s, sep string, n int) []string

[html] view plaincopy
  1. func main() {
  2. s := "Hello, 世界! Hello!"
  3. ss := strings.SplitN(s, " ", 2)
  4. fmt.Printf("%q\n", ss) // ["Hello," "世界! Hello!"]
  5. ss = strings.SplitN(s, " ", -1)
  6. fmt.Printf("%q\n", ss) // ["Hello," "世界!" "Hello!"]
  7. ss = strings.SplitN(s, "", 3)
  8. fmt.Printf("%q\n", ss) // ["H" "e" "llo, 世界! Hello!"]
  9. }

------------------------------------------------------------

// SplitAfterN 以 sep 为分隔符,将 s 切分成多个子串,结果中包含 sep 本身
// 如果 sep 为空,则将 s 切分成 Unicode 字符列表。
// 如果 s 中没有 sep 子串,则将整个 s 作为 []string 的第一个元素返回
// 参数 n 表示最多切分出几个子串,超出的部分将不再切分。
// 如果 n 为 0,则返回 nil,如果 n 小于 0,则不限制切分个数,全部切分
func SplitAfterN(s, sep string, n int) []string

[html] view plaincopy
  1. func main() {
  2. s := "Hello, 世界! Hello!"
  3. ss := strings.SplitAfterN(s, " ", 2)
  4. fmt.Printf("%q\n", ss) // ["Hello, " "世界! Hello!"]
  5. ss = strings.SplitAfterN(s, " ", -1)
  6. fmt.Printf("%q\n", ss) // ["Hello, " "世界! " "Hello!"]
  7. ss = strings.SplitAfterN(s, "", 3)
  8. fmt.Printf("%q\n", ss) // ["H" "e" "llo, 世界! Hello!"]
  9. }

------------------------------------------------------------

// Split 以 sep 为分隔符,将 s 切分成多个子切片,结果中不包含 sep 本身
// 如果 sep 为空,则将 s 切分成 Unicode 字符列表。
// 如果 s 中没有 sep 子串,则将整个 s 作为 []string 的第一个元素返回
func Split(s, sep string) []string

[html] view plaincopy
  1. func main() {
  2. s := "Hello, 世界! Hello!"
  3. ss := strings.Split(s, " ")
  4. fmt.Printf("%q\n", ss) // ["Hello," "世界!" "Hello!"]
  5. ss = strings.Split(s, ", ")
  6. fmt.Printf("%q\n", ss) // ["Hello" "世界! Hello!"]
  7. ss = strings.Split(s, "")
  8. fmt.Printf("%q\n", ss) // 单个字符列表
  9. }

------------------------------------------------------------

// SplitAfter 以 sep 为分隔符,将 s 切分成多个子切片,结果中包含 sep 本身
// 如果 sep 为空,则将 s 切分成 Unicode 字符列表。
// 如果 s 中没有 sep 子串,则将整个 s 作为 []string 的第一个元素返回
func SplitAfter(s, sep string) []string

[html] view plaincopy
  1. func main() {
  2. s := "Hello, 世界! Hello!"
  3. ss := strings.SplitAfter(s, " ")
  4. fmt.Printf("%q\n", ss) // ["Hello, " "世界! " "Hello!"]
  5. ss = strings.SplitAfter(s, ", ")
  6. fmt.Printf("%q\n", ss) // ["Hello, " "世界! Hello!"]
  7. ss = strings.SplitAfter(s, "")
  8. fmt.Printf("%q\n", ss) // 单个字符列表
  9. }

------------------------------------------------------------

// Fields 以连续的空白字符为分隔符,将 s 切分成多个子串,结果中不包含空白字符本身
// 空白字符有:\t, \n, \v, \f, \r, ' ', U+0085 (NEL), U+00A0 (NBSP)
// 如果 s 中只包含空白字符,则返回一个空列表
func Fields(s string) []string

[html] view plaincopy
  1. func main() {
  2. s := "Hello, 世界! Hello!"
  3. ss := strings.Fields(s)
  4. fmt.Printf("%q\n", ss) // ["Hello," "世界!" "Hello!"]
  5. }

------------------------------------------------------------

// FieldsFunc 以一个或多个满足 f(rune) 的字符为分隔符,
// 将 s 切分成多个子串,结果中不包含分隔符本身。
// 如果 s 中没有满足 f(rune) 的字符,则返回一个空列表。
func FieldsFunc(s string, f func(rune) bool) []string

[html] view plaincopy
  1. func isSlash(r rune) bool {
  2. return r == '\\' || r == '/'
  3. }
  4. func main() {
  5. s := "C:\\Windows\\System32\\FileName"
  6. ss := strings.FieldsFunc(s, isSlash)
  7. fmt.Printf("%q\n", ss) // ["C:" "Windows" "System32" "FileName"]
  8. }

------------------------------------------------------------

// Join 将 a 中的子串连接成一个单独的字符串,子串之间用 sep 分隔
func Join(a []string, sep string) string

[html] view plaincopy
  1. func main() {
  2. ss := []string{"Monday", "Tuesday", "Wednesday"}
  3. s := strings.Join(ss, "|")
  4. fmt.Println(s)
  5. }

------------------------------------------------------------

// HasPrefix 判断字符串 s 是否以 prefix 开头
func HasPrefix(s, prefix string) bool

[html] view plaincopy
  1. func main() {
  2. s := "Hello 世界!"
  3. b := strings.HasPrefix(s, "hello")
  4. fmt.Println(b) // false
  5. b = strings.HasPrefix(s, "Hello")
  6. fmt.Println(b) // true
  7. }

------------------------------------------------------------

// HasSuffix 判断字符串 s 是否以 prefix 结尾
func HasSuffix(s, suffix string) bool

[html] view plaincopy
  1. func main() {
  2. s := "Hello 世界!"
  3. b := strings.HasSuffix(s, "世界")
  4. fmt.Println(b) // false
  5. b = strings.HasSuffix(s, "世界!")
  6. fmt.Println(b) // true
  7. }

------------------------------------------------------------

// Map 将 s 中满足 mapping(rune) 的字符替换为 mapping(rune) 的返回值。
// 如果 mapping(rune) 返回负数,则相应的字符将被删除。
func Map(mapping func(rune) rune, s string) string

[html] view plaincopy
  1. func Slash(r rune) rune {
  2. if r == '\\' {
  3. return '/'
  4. }
  5. return r
  6. }
  7. func main() {
  8. s := "C:\\Windows\\System32\\FileName"
  9. ms := strings.Map(Slash, s)
  10. fmt.Printf("%q\n", ms) // "C:/Windows/System32/FileName"
  11. }

------------------------------------------------------------

// Repeat 将 count 个字符串 s 连接成一个新的字符串
func Repeat(s string, count int) string

[html] view plaincopy
  1. func main() {
  2. s := "Hello!"
  3. rs := strings.Repeat(s, 3)
  4. fmt.Printf("%q\n", rs) // "Hello!Hello!Hello!"
  5. }

------------------------------------------------------------

// ToUpper 将 s 中的所有字符修改为其大写格式
// 对于非 ASCII 字符,它的大写格式需要查表转换
func ToUpper(s string) string

// ToLower 将 s 中的所有字符修改为其小写格式
// 对于非 ASCII 字符,它的小写格式需要查表转换
func ToLower(s string) string

// ToTitle 将 s 中的所有字符修改为其 Title 格式
// 大部分字符的 Title 格式就是其 Upper 格式
// 只有少数字符的 Title 格式是特殊字符
// 这里的 ToTitle 主要给 Title 函数调用
func ToTitle(s string) string

[html] view plaincopy
  1. func main() {
  2. s := "heLLo worLd Abc"
  3. us := strings.ToUpper(s)
  4. ls := strings.ToLower(s)
  5. ts := strings.ToTitle(s)
  6. fmt.Printf("%q\n", us) // "HELLO WORLD ABC"
  7. fmt.Printf("%q\n", ls) // "hello world abc"
  8. fmt.Printf("%q\n", ts) // "HELLO WORLD ABC"
  9. }

// 获取非 ASCII 字符的 Title 格式列表

[html] view plaincopy
  1. func main() {
  2. for _, cr := range unicode.CaseRanges {
  3. // u := uint32(cr.Delta[unicode.UpperCase]) // 大写格式
  4. // l := uint32(cr.Delta[unicode.LowerCase]) // 小写格式
  5. t := uint32(cr.Delta[unicode.TitleCase]) // Title 格式
  6. // if t != 0 && t != u {
  7. if t != 0 {
  8. for i := cr.Lo; i <= cr.Hi; i++ {
  9. fmt.Printf("%c -> %c\n", i, i+t)
  10. }
  11. }
  12. }
  13. }

------------------------------------------------------------

// ToUpperSpecial 将 s 中的所有字符修改为其大写格式。
// 优先使用 _case 中的规则进行转换
func ToUpperSpecial(_case unicode.SpecialCase, s string) string

// ToLowerSpecial 将 s 中的所有字符修改为其小写格式。
// 优先使用 _case 中的规则进行转换
func ToLowerSpecial(_case unicode.SpecialCase, s string) string

// ToTitleSpecial 将 s 中的所有字符修改为其 Title 格式。
// 优先使用 _case 中的规则进行转换
func ToTitleSpecial(_case unicode.SpecialCase, s string) string

_case 规则说明,以下列语句为例:
unicode.CaseRange{'A', 'Z', [unicode.MaxCase]rune{3, -3, 0}}
·其中 'A', 'Z' 表示此规则只影响 'A' 到 'Z' 之间的字符。
·其中 [unicode.MaxCase]rune 数组表示:
当使用 ToUpperSpecial 转换时,将字符的 Unicode 编码与第一个元素值(3)相加
当使用 ToLowerSpecial 转换时,将字符的 Unicode 编码与第二个元素值(-3)相加
当使用 ToTitleSpecial 转换时,将字符的 Unicode 编码与第三个元素值(0)相加

[html] view plaincopy
  1. func main() {
  2. // 定义转换规则
  3. var _MyCase = unicode.SpecialCase{
  4. // 将半角逗号替换为全角逗号,ToTitle 不处理
  5. unicode.CaseRange{',', ',',
  6. [unicode.MaxCase]rune{',' - ',', ',' - ',', 0}},
  7. // 将半角句号替换为全角句号,ToTitle 不处理
  8. unicode.CaseRange{'.', '.',
  9. [unicode.MaxCase]rune{'。' - '.', '。' - '.', 0}},
  10. // 将 ABC 分别替换为全角的 ABC、abc,ToTitle 不处理
  11. unicode.CaseRange{'A', 'C',
  12. [unicode.MaxCase]rune{'A' - 'A', 'a' - 'A', 0}},
  13. }
  14. s := "ABCDEF,abcdef."
  15. us := strings.ToUpperSpecial(_MyCase, s)
  16. fmt.Printf("%q\n", us) // "ABCDEF,ABCDEF。"
  17. ls := strings.ToLowerSpecial(_MyCase, s)
  18. fmt.Printf("%q\n", ls) // "abcdef,abcdef。"
  19. ts := strings.ToTitleSpecial(_MyCase, s)
  20. fmt.Printf("%q\n", ts) // "ABCDEF,ABCDEF."
  21. }

------------------------------------------------------------

// Title 将 s 中的所有单词的首字母修改为其 Title 格式
// BUG: Title 规则不能正确处理 Unicode 标点符号
func Title(s string) string

[html] view plaincopy
  1. func main() {
  2. s := "heLLo worLd"
  3. ts := strings.Title(s)
  4. fmt.Printf("%q\n", ts) // "HeLLo WorLd"
  5. }

------------------------------------------------------------

// TrimLeftFunc 将删除 s 头部连续的满足 f(rune) 的字符
func TrimLeftFunc(s string, f func(rune) bool) string

------------------------------------------------------------

// TrimRightFunc 将删除 s 尾部连续的满足 f(rune) 的字符
func TrimRightFunc(s string, f func(rune) bool) string

[html] view plaincopy
  1. func isSlash(r rune) bool {
  2. return r == '\\' || r == '/'
  3. }
  4. func main() {
  5. s := "\\\\HostName\\C\\Windows\\"
  6. ts := strings.TrimRightFunc(s, isSlash)
  7. fmt.Printf("%q\n", ts) // "\\\\HostName\\C\\Windows"
  8. }

------------------------------------------------------------

// TrimFunc 将删除 s 首尾连续的满足 f(rune) 的字符
func TrimFunc(s string, f func(rune) bool) string

[html] view plaincopy
  1. func isSlash(r rune) bool {
  2. return r == '\\' || r == '/'
  3. }
  4. func main() {
  5. s := "\\\\HostName\\C\\Windows\\"
  6. ts := strings.TrimFunc(s, isSlash)
  7. fmt.Printf("%q\n", ts) // "HostName\\C\\Windows"
  8. }

------------------------------------------------------------

// 返回 s 中第一个满足 f(rune) 的字符的字节位置。
// 如果没有满足 f(rune) 的字符,则返回 -1
func IndexFunc(s string, f func(rune) bool) int

[html] view plaincopy
  1. func isSlash(r rune) bool {
  2. return r == '\\' || r == '/'
  3. }
  4. func main() {
  5. s := "C:\\Windows\\System32"
  6. i := strings.IndexFunc(s, isSlash)
  7. fmt.Printf("%v\n", i) // 2
  8. }

------------------------------------------------------------

// 返回 s 中最后一个满足 f(rune) 的字符的字节位置。
// 如果没有满足 f(rune) 的字符,则返回 -1
func LastIndexFunc(s string, f func(rune) bool) int

[html] view plaincopy
  1. func isSlash(r rune) bool {
  2. return r == '\\' || r == '/'
  3. }
  4. func main() {
  5. s := "C:\\Windows\\System32"
  6. i := strings.LastIndexFunc(s, isSlash)
  7. fmt.Printf("%v\n", i) // 10
  8. }

------------------------------------------------------------

// Trim 将删除 s 首尾连续的包含在 cutset 中的字符
func Trim(s string, cutset string) string

[html] view plaincopy
  1. func main() {
  2. s := " Hello 世界! "
  3. ts := strings.Trim(s, " Helo!")
  4. fmt.Printf("%q\n", ts) // "世界"
  5. }

------------------------------------------------------------

// TrimLeft 将删除 s 头部连续的包含在 cutset 中的字符
func TrimLeft(s string, cutset string) string

[html] view plaincopy
  1. func main() {
  2. s := " Hello 世界! "
  3. ts := strings.TrimLeft(s, " Helo")
  4. fmt.Printf("%q\n", ts) // "世界! "
  5. }

------------------------------------------------------------

// TrimRight 将删除 s 尾部连续的包含在 cutset 中的字符
func TrimRight(s string, cutset string) string

[html] view plaincopy
  1. func main() {
  2. s := " Hello 世界! "
  3. ts := strings.TrimRight(s, " 世界!")
  4. fmt.Printf("%q\n", ts) // " Hello"
  5. }

------------------------------------------------------------

// TrimSpace 将删除 s 首尾连续的的空白字符
func TrimSpace(s string) string

[html] view plaincopy
  1. func main() {
  2. s := " Hello 世界! "
  3. ts := strings.TrimSpace(s)
  4. fmt.Printf("%q\n", ts) // "Hello 世界!"
  5. }

------------------------------------------------------------

// TrimPrefix 删除 s 头部的 prefix 字符串
// 如果 s 不是以 prefix 开头,则返回原始 s
func TrimPrefix(s, prefix string) string

[html] view plaincopy
  1. func main() {
  2. s := "Hello 世界!"
  3. ts := strings.TrimPrefix(s, "Hello")
  4. fmt.Printf("%q\n", ts) // " 世界"
  5. }

------------------------------------------------------------

// TrimSuffix 删除 s 尾部的 suffix 字符串
// 如果 s 不是以 suffix 结尾,则返回原始 s
func TrimSuffix(s, suffix string) string

[html] view plaincopy
  1. func main() {
  2. s := "Hello 世界!!!!!"
  3. ts := strings.TrimSuffix(s, "!!!!")
  4. fmt.Printf("%q\n", ts) // " 世界"
  5. }

注:TrimSuffix只是去掉s字符串结尾的suffix字符串,只是去掉1次,而TrimRight是一直去掉s字符串右边的字符串,只要有响应的字符串就去掉,是一个多次的过程,这也是二者的本质区别.

------------------------------------------------------------

// Replace 返回 s 的副本,并将副本中的 old 字符串替换为 new 字符串
// 替换次数为 n 次,如果 n 为 -1,则全部替换
// 如果 old 为空,则在副本的每个字符之间都插入一个 new
func Replace(s, old, new string, n int) string

[html] view plaincopy
  1. func main() {
  2. s := "Hello 世界!"
  3. s = strings.Replace(s, " ", ",", -1)
  4. fmt.Println(s)
  5. s = strings.Replace(s, "", "|", -1)
  6. fmt.Println(s)
  7. }

------------------------------------------------------------

// EqualFold 判断 s 和 t 是否相等。忽略大小写,同时它还会对特殊字符进行转换
// 比如将“ϕ”转换为“Φ”、将“DŽ”转换为“Dž”等,然后再进行比较
func EqualFold(s, t string) bool

[html] view plaincopy
  1. func main() {
  2. s1 := "Hello 世界! ϕ DŽ"
  3. s2 := "hello 世界! Φ Dž"
  4. b := strings.EqualFold(s1, s2)
  5. fmt.Printf("%v\n", b) // true
  6. }

============================================================

// reader.go

------------------------------------------------------------

// Reader 结构通过读取字符串,实现了 io.Reader,io.ReaderAt,
// io.Seeker,io.WriterTo,io.ByteScanner,io.RuneScanner 接口
type Reader struct {
s string // 要读取的字符串
i int // 当前读取的索引位置,从 i 处开始读取数据
prevRune int // 读取的前一个字符的索引位置,小于 0 表示之前未读取字符
}

// 通过字符串 s 创建 strings.Reader 对象
// 这个函数类似于 bytes.NewBufferString
// 但比 bytes.NewBufferString 更有效率,而且只读
func NewReader(s string) *Reader { return &Reader{s, 0, -1} }

------------------------------------------------------------

// Len 返回 r.i 之后的所有数据的字节长度
func (r *Reader) Len() int

[html] view plaincopy
  1. func main() {
  2. s := "Hello 世界!"
  3. // 创建 Reader
  4. r := strings.NewReader(s)
  5. // 获取字符串的编码长度
  6. fmt.Println(r.Len()) // 13
  7. }

------------------------------------------------------------

// Read 将 r.i 之后的所有数据写入到 b 中(如果 b 的容量足够大)
// 返回读取的字节数和读取过程中遇到的错误
// 如果无可读数据,则返回 io.EOF
func (r *Reader) Read(b []byte) (n int, err error)

[html] view plaincopy
  1. func main() {
  2. s := "Hello World!"
  3. // 创建 Reader
  4. r := strings.NewReader(s)
  5. // 创建长度为 5 个字节的缓冲区
  6. b := make([]byte, 5)
  7. // 循环读取 r 中的字符串
  8. for n, _ := r.Read(b); n > 0; n, _ = r.Read(b) {
  9. fmt.Printf("%q, ", b[:n]) // "Hello", " Worl", "d!"
  10. }
  11. }

------------------------------------------------------------

// ReadAt 将 off 之后的所有数据写入到 b 中(如果 b 的容量足够大)
// 返回读取的字节数和读取过程中遇到的错误
// 如果无可读数据,则返回 io.EOF
// 如果数据被一次性读取完毕,则返回 io.EOF
func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)

[html] view plaincopy
  1. func main() {
  2. s := "Hello World!"
  3. // 创建 Reader
  4. r := strings.NewReader(s)
  5. // 创建长度为 5 个字节的缓冲区
  6. b := make([]byte, 5)
  7. // 读取 r 中指定位置的字符串
  8. n, _ := r.ReadAt(b, 0)
  9. fmt.Printf("%q\n", b[:n]) // "Hello"
  10. // 读取 r 中指定位置的字符串
  11. n, _ = r.ReadAt(b, 6)
  12. fmt.Printf("%q\n", b[:n]) // "World"
  13. }

------------------------------------------------------------

// ReadByte 将 r.i 之后的一个字节写入到返回值 b 中
// 返回读取的字节和读取过程中遇到的错误
// 如果无可读数据,则返回 io.EOF
func (r *Reader) ReadByte() (b byte, err error)

[html] view plaincopy
  1. func main() {
  2. s := "Hello World!"
  3. // 创建 Reader
  4. r := strings.NewReader(s)
  5. // 读取 r 中的一个字节
  6. for i := 0; i < 3; i++ {
  7. b, _ := r.ReadByte()
  8. fmt.Printf("%q, ", b) // 'H', 'e', 'l',
  9. }
  10. }

------------------------------------------------------------

// UnreadByte 撤消前一次的 ReadByte 操作,即 r.i--
func (r *Reader) UnreadByte() error

[html] view plaincopy
  1. func main() {
  2. s := "Hello World!"
  3. // 创建 Reader
  4. r := strings.NewReader(s)
  5. // 读取 r 中的一个字节
  6. for i := 0; i < 3; i++ {
  7. b, _ := r.ReadByte()
  8. fmt.Printf("%q, ", b) // 'H', 'H', 'H',
  9. r.UnreadByte()        // 撤消前一次的字节读取操作
  10. }
  11. }

------------------------------------------------------------

// ReadRune 将 r.i 之后的一个字符写入到返回值 ch 中
// ch: 读取的字符
// size:ch 的编码长度
// err: 读取过程中遇到的错误
// 如果无可读数据,则返回 io.EOF
// 如果 r.i 之后不是一个合法的 UTF-8 字符编码,则返回 utf8.RuneError 字符
func (r *Reader) ReadRune() (ch rune, size int, err error)

[html] view plaincopy
  1. func main() {
  2. s := "你好 世界!"
  3. // 创建 Reader
  4. r := strings.NewReader(s)
  5. // 读取 r 中的一个字符
  6. for i := 0; i < 5; i++ {
  7. b, n, _ := r.ReadRune()
  8. fmt.Printf(`"%c:%v", `, b, n)
  9. // "你:3", "好:3", " :1", "世:3", "界:3",
  10. }
  11. }

------------------------------------------------------------

// 撤消前一次的 ReadRune 操作
func (r *Reader) UnreadRune() error

[html] view plaincopy
  1. func main() {
  2. s := "你好 世界!"
  3. // 创建 Reader
  4. r := strings.NewReader(s)
  5. // 读取 r 中的一个字符
  6. for i := 0; i < 5; i++ {
  7. b, _, _ := r.ReadRune()
  8. fmt.Printf("%q, ", b)
  9. // '你', '你', '你', '你', '你',
  10. r.UnreadRune() // 撤消前一次的字符读取操作
  11. }
  12. }

------------------------------------------------------------

// Seek 用来移动 r 中的索引位置
// offset:要移动的偏移量,负数表示反向移动
// whence:从那里开始移动,0:起始位置,1:当前位置,2:结尾位置
// 如果 whence 不是 0、1、2,则返回错误信息
// 如果目标索引位置超出字符串范围,则返回错误信息
// 目标索引位置不能超出 1 << 31,否则返回错误信息
func (r *Reader) Seek(offset int64, whence int) (int64, error)

[html] view plaincopy
  1. func main() {
  2. s := "Hello World!"
  3. // 创建 Reader
  4. r := strings.NewReader(s)
  5. // 创建读取缓冲区
  6. b := make([]byte, 5)
  7. // 读取 r 中指定位置的内容
  8. r.Seek(6, 0) // 移动索引位置到第 7 个字节
  9. r.Read(b)    // 开始读取
  10. fmt.Printf("%q\n", b)
  11. r.Seek(-5, 1) // 将索引位置移回去
  12. r.Read(b)     // 继续读取
  13. fmt.Printf("%q\n", b)
  14. }

------------------------------------------------------------

// WriteTo 将 r.i 之后的数据写入接口 w 中
func (r *Reader) WriteTo(w io.Writer) (n int64, err error)

[html] view plaincopy
  1. func main() {
  2. s := "Hello World!"
  3. // 创建 Reader
  4. r := strings.NewReader(s)
  5. // 创建 bytes.Buffer 对象,它实现了 io.Writer 接口
  6. buf := bytes.NewBuffer(nil)
  7. // 将 r 中的数据写入 buf 中
  8. r.WriteTo(buf)
  9. fmt.Printf("%q\n", buf) // "Hello World!"
  10. }

============================================================

// replace.go

------------------------------------------------------------

// Replacer 根据一个替换列表执行替换操作
type Replacer struct {
Replace(s string) string
WriteString(w io.Writer, s string) (n int, err error)
}

------------------------------------------------------------

// NewReplacer 通过“替换列表”创建一个 Replacer 对象。
// 按照“替换列表”中的顺序进行替换,只替换非重叠部分。
// 如果参数的个数不是偶数,则抛出异常。
// 如果在“替换列表”中有相同的“查找项”,则后面重复的“查找项”会被忽略
func NewReplacer(oldnew ...string) *Replacer

------------------------------------------------------------

// Replace 返回对 s 进行“查找和替换”后的结果
// Replace 使用的是 Boyer-Moore 算法,速度很快
func (r *Replacer) Replace(s string) string

[html] view plaincopy
  1. func main() {
  2. srp := strings.NewReplacer("Hello", "你好", "World", "世界", "!", "!")
  3. s := "Hello World!Hello World!hello world!"
  4. rst := srp.Replace(s)
  5. fmt.Print(rst) // 你好 世界!你好 世界!hello world!
  6. }
  7. <span style="color:#FF0000;">注:这两种写法均可.</span>
  8. func main() {
  9. wl := []string{"Hello", "Hi", "Hello", "你好"}
  10. srp := strings.NewReplacer(wl...)
  11. s := "Hello World! Hello World! hello world!"
  12. rst := srp.Replace(s)
  13. fmt.Print(rst) // Hi World! Hi World! hello world!
  14. }

------------------------------------------------------------

// WriteString 对 s 进行“查找和替换”,然后将结果写入 w 中
func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)

[html] view plaincopy
  1. func main() {
  2. wl := []string{"Hello", "你好", "World", "世界", "!", "!"}
  3. srp := strings.NewReplacer(wl...)
  4. s := "Hello World!Hello World!hello world!"
  5. srp.WriteString(os.Stdout, s)
  6. // 你好 世界!你好 世界!hello world!
  7. }

go详解strings包相关推荐

  1. 详解xlwings包,用Python代替Excel VBA

    详解xlwings包,用Python代替Excel VBA <代替VBA! 用Python轻松实现Excel编程>demo 主要内容 Python语法基础 Excel对象模型:OpenPy ...

  2. go详解bufio包

    // bufio 包实现了带缓存的 I/O 操作 // 它封装一个 io.Reader 或 io.Writer 对象 // 使其具有缓存和一些文本读写功能 ---------------------- ...

  3. 详解程序包管理RPM

    一.定义 RPM是RPM Package Manager(RPM软件包管理器)的缩写,这一文件格式名称虽然打上了RedHat的标志,但是其原始设计理念是开放式的,现在包括OpenLinux.S.u.S ...

  4. pythonpackage详解_Python详解之包管理:__init__.py

    Python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文件,在另一个文件中需要import时,将事先写好的.py文件拷贝到当前目录,或者是在sys.path中增加事先写好的.p ...

  5. [转]详解HTTP包

    一.超文本传输协议及HTTP包     HTTP协议用于在Internet上发送和接收消息.HTTP协议是一种请求-应答式的协议--客户端发送一个请求,服务器返回该请求的应答,所有的请求与应答都是HT ...

  6. windows smb更改端口_SMB协议(使用说明+过程详解+抓包分析)

    一.SMB概述 SMB(ServerMessage Block)通信协议是微软(Microsoft)和英特尔(Intel)在1987年制定的协议,主要是作为Microsoft网络的通讯协议.SMB 是 ...

  7. 汉诺塔详解(包看包会)

    CSDN的大佬已经解释了很多了,由我这个菜鸟反复理解后得到的一些心得的分享 先看题: 汉诺塔: 汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具.大梵天创造世界的时候做了三根金刚石柱子,在一根 ...

  8. 13. 软件包详解,rpm包的查找,安装,升级,卸载,验证等所有操作

    本小节会详细介绍linux中的软件包管理,涉及软件包的定义/安装/卸载/依赖等操作.以及会详细演示rpm这个非常重要命令的使用. 文章目录 前言 软件包 源码包 二进制包 源码包 VS二进制包 依赖性 ...

  9. 详解jar包的启动命令

    通常我们在启动SpringBoot项目的的jar包时,会使用以下命令 nohup java -jar xxxx.jar >log.log 2>&1 & 整条命令由linux ...

最新文章

  1. 纽大副教授炮轰NeurIPS、AAAI等顶会:无聊、就不该继续存在
  2. 麦肯锡季刊 | 人工智能的发展与障碍
  3. 高手是如何定位内存问题的代码位置的
  4. 基于 Rancher 的企业 CI/CD 环境搭建
  5. shiroConfig配置中要注意的事项
  6. python程序 爱意_程序员式优雅表白,教你用python代码画爱心
  7. javaone_JavaOne 2012 – 2400小时! 一些建议
  8. 项目内置广告后续:npm 禁止终端广告
  9. C语言 pthread_exit
  10. 正式发布 .Net2.0 大文件上传服务器控件
  11. 关于Cocos2d-x的粒子系统
  12. DDR3:MIG控制器设计(vivado)
  13. 明解C语言 【日】 柴田望洋 第十章 指针 代码清单和练习代码
  14. ES数据库重建索引——Reindex(数据迁移)
  15. 【bug】vue.runtime.esm.js?2b0e:619 [Vue warn]: Failed to mount component: template or render function
  16. 如何给电脑安装双系统
  17. 网络层协议和数据链路层协议
  18. 详解:海盗分赃(25 分)
  19. 如何提高Bug敏感度
  20. “排队” 用英语怎么说

热门文章

  1. python怎么写微分方程_python微分方程
  2. 计算机系统-实模式/保护模式/虚拟86模式
  3. SpringCloud-Config
  4. MIT6.830 Lab3 Query Optimization 实验报告
  5. 2.7_single_link_list_单链表
  6. php 小墙 垃圾评论,关于php过滤垃圾评论
  7. master节点重置后添加node报错_超强教程!在树莓派上构建多节点K8S集群!
  8. 第5堂:看到词句就会读-上
  9. 开发中用到过的技术链接
  10. Udp---模拟实现客户端与服务器通信