我如何使用bufio.ScanWords
和bufio.ScanLines
函数来计算单词和行?
我试过了:
fmt.Println(bufio.ScanWords([]byte("Good day everyone"), false))
Run Code Online (Sandbox Code Playgroud)
打印:
5 [103 111 111 100] <nil>
Run Code Online (Sandbox Code Playgroud)
不确定那是什么意思?
Ale*_*mov 11
计算单词:
input := "Spicy jalapeno pastrami ut ham turducken.\n Lorem sed ullamco, leberkas sint short loin strip steak ut shoulder shankle porchetta venison prosciutto turducken swine.\n Deserunt kevin frankfurter tongue aliqua incididunt tri-tip shank nostrud.\n"
scanner := bufio.NewScanner(strings.NewReader(input))
// Set the split function for the scanning operation.
scanner.Split(bufio.ScanWords)
// Count the words.
count := 0
for scanner.Scan() {
count++
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading input:", err)
}
fmt.Printf("%d\n", count)
Run Code Online (Sandbox Code Playgroud)
计算线数:
input := "Spicy jalapeno pastrami ut ham turducken.\n Lorem sed ullamco, leberkas sint short loin strip steak ut shoulder shankle porchetta venison prosciutto turducken swine.\n Deserunt kevin frankfurter tongue aliqua incididunt tri-tip shank nostrud.\n"
scanner := bufio.NewScanner(strings.NewReader(input))
// Set the split function for the scanning operation.
scanner.Split(bufio.ScanLines)
// Count the words.
count := 0
for scanner.Scan() {
count++
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading input:", err)
}
fmt.Printf("%d\n", count)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1339 次 |
最近记录: |