Go题库4_字符串分隔

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    var b strings.Builder

    for scanner.Scan() {
        b.Reset() // 置空
        str := scanner.Text()
        times := 0
        if len(str) == 0 {
            break
        }

        for i := 0; i < len(str); i++ {
            times++
            b.WriteByte(str[i]) // 拼接
            if times == 8 {
                fmt.Println(b.String())
                times = 0
                b = strings.Builder{} // 置空
            }
        }

        if times > 0 {
            for i := times; i < 8; i++ {
                b.WriteString("0") // 拼接
            }
            fmt.Println(b.String())
        }

    }

}
原文地址:https://www.cnblogs.com/luwei0915/p/15509722.html