golang扫描器读取直到读取器结尾

我在StringReader上有一个bufio扫描器。 当我到达扫描仪输出的某一行后,我想阅读直到阅读器的末尾。 除了注释的代码以外,还有什么方法可以使用更简单的方法来实现此目的?</ p>

  s:=1 
2
3
4
5
6
7

beyond5:=“”
scanner:= bufio.NewScanner(strings.NewReader(s))
for Scanner.Scan(){
如果是string.Contains(scanner.Text(),“ 5”){\ n //从扫描仪读取所有行,直到EOF为止// n //并将其存储在beyond5
//中供scan.Scan(){
// beyond5 + = scaner.Text()
// beyond5 + =“ \ n“
//}
break
}
}

log.Println(beyond5)
</ code> </ pre>
</ div>

展开原文

原文

I have a bufio scanner on a StringReader. After I reach a certain line on the Scanner output, I want to read until the end of the reader. Is there any way to achieve this using a simpler way, other than the commented code ?

s := `1
2
3
4
5
6
7`

beyond5 := ""
scanner := bufio.NewScanner(strings.NewReader(s))
for scanner.Scan() {
    if strings.Contains(scanner.Text(), "5") {
        // Read all lines until EOF from scanner
        // and store in beyond5
        // for scanner.Scan() {
        //  beyond5 += scanner.Text()
        //  beyond5 += "
"
        // }
        break
    }
}

log.Println(beyond5)

dongzouhe9734
dongzouhe9734 您也许可以使用自定义拆分功能:play.golang.org/p/sr1pUIR3NC但是,这可能仍无法对EOF执行完全查找,并且不再在 上拆分。
4 年多之前 回复

2个回答



似乎使用扫描仪根本无法进行这种操作。 我们只需要使用bufio阅读器。 代码是:</ p>

  s:=1 
2
3
4
5
6
7

beyond5:=“”
r:=字符串。 NewReader(s)
reader:= bufio.NewReader(r)
for {
行,err:= reader.ReadString('
')
如果err!= nil {
log.Fatal(err)\ n}
if strings.Contains(line,“ 5”){
b,_:= ioutil.ReadAll(reader)
beyond5 =字符串(b)
break
}
}

log。 Println(beyond5)
</ code> </ pre>
</ div>

展开原文

原文

It seems such an operation is not possible at all with the scanner. We need to use the bufio reader only. The code is:

s := `1
2
3
4
5
6
7`

beyond5 := ""
r := strings.NewReader(s)
reader := bufio.NewReader(r)
for {
    line, err := reader.ReadString('
')
    if err != nil {
        log.Fatal(err)
    }
    if strings.Contains(line, "5") {
        b, _ := ioutil.ReadAll(reader)
        beyond5 = string(b)
        break
    }
}

log.Println(beyond5)

Is this that you want? :-)

    s := `1
          2
          3
          4
          5
          6
          7`

    var beyond5 string

    if strings.Contains(s, "5") {
       scanner := bufio.NewScanner(strings.NewReader(s))
       for scanner.Scan() {
          beyond5 += scanner.Text()
        }
    }

    beyond5 += "
"

    log.Println(beyond5)

UPDATE: My solution is similar to @abhink's solution, however, they are different solutions

I have used Split method, this method set split (private) property that it is SplitFunc type. And this is my implementation:

var canPass bool

func mySplit(data []byte, atEOF bool) (advance int, token []byte, err error) {
    for pos, value := range data {

        if (value < '6' && !canPass) || (value < '0' || value > '9') {
            continue
        }

        canPass = true
        return pos+1, data[pos : pos + 1], nil
    }

    return 0, nil, nil
}

Now, you must use Split method, send it mySplit as parameter.

scanner.Split(mySplit)

And you do a simple for with scanner.Scan()

for scanner.Scan() {
        beyond5 += scanner.Text() + "
"
}

Output:

6
7

Playground

I hope it is the solution that you were looking for :-)

duankuixi1930
duankuixi1930 我已经更新了解决方案。 见更新
4 年多之前 回复
dourui7186
dourui7186 不,这不是我想要的。 我希望打印“ 6 7”。 但更重要的是,我想知道是否有一种方法可以从扫描仪的SEEK_CUR位置读取EOF。
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐