一、函数说明 strings.NewReader创建一个从s读取数据的Reader type Reader struct { s string //对应的字符串 i int64 // 当前读取到的位置 p
strings.NewReader创建一个从s读取数据的Reader
type Reader struct {
s string //对应的字符串
i int64 // 当前读取到的位置
prevRune int
}
// 读取器实了 io.Reader, io.ReaderAt, io.Seeker, io.WriterTo,
// io.ByteScanner, 和 io.RuneScanner 通过从字符串中读取
// Reader的零值类似于空字符串的读取器
type Reader struct {
s string // 对应的字符串
i int64 // 当前读取到的位置
prevRune int // prevRune索引; or < 0
}
func (r *Reader) ReadByte() (byte, error)
r *Reader
中读出一个字节并返回r *Reader
中无可读数据,则返回一个错误func (r *Reader) UnreadByte() error
func (r *Reader) ReadByte() (byte, error)
代码示例:
read := strings.NewReader("abcd")
c, _ := read.ReadByte()
fmt.Printf("%c\n", c) // a
c, _ = read.ReadByte()
fmt.Printf("%c\n", c) // b
_ = read.UnreadByte()
c, _ = read.ReadByte() // b
fmt.Printf("%c\n", c)
方法说明:
func (r *Reader) ReadRune() (ch rune, size int, err error)
r *Reader
中读出一个 UTF8
编码的字符串并返回第一个参数 ch runesize int
,如果发生错误,第三个参数即错误 err error
UTF8
序列无法解码出一个正确的 Unicode
字符,只读出 r *Reader
中的一个字节,并返回 ch = U+FFFD 字符,size = 1func (r *Reader) UnreadRune() error
UnreadRune
撤消最后一次读出的 Unicode
字符ReadRune
操作,则返回一个错误UnreadRune
比 UnreadByte
更加严格代码示例:
reader := strings.NewReader("你好,世界!")
s, size, _ := reader.ReadRune()
fmt.Printf("%c %v \n", s, size) // 你 3
_ = reader.UnreadRune()
s, size, _ = reader.ReadRune()
fmt.Printf("%c %v \n", s, size) // 你 3
实现类似于 PHP 的 mb_strpos
函数:
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(mb_strpos("聚合数据科技公司", "科技")) // 4
fmt.Println(mb_strpos("聚合数据--科技公司", "科技")) // 6
fmt.Println(mb_strpos("juhe聚合数据-科技公司", "数据")) // 6
}
func mb_strpos(haystack, needle string) int {
index := strings.Index(haystack, needle)
if index == -1 || index == 0 {
return index
}
pos := 0
total := 0
reader := strings.NewReader(haystack)
for {
_, size, err := reader.ReadRune()
if err != nil {
return -1
}
total += size
pos++
// Got it
if total == index {
return pos
}
}
}
--结束END--
本文标题: Go语言 strings包NewReader方法及ReadRune查找字符串
本文链接: https://www.lsjlt.com/news/99265.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0