-2

I have a string like "test123abc45alsdkfj", I want my scanner to behave such that it read "test" first, then 123, then "abc", then 45, then "alsdkfj". Kinda like stringstream in C++, is there a way to do this? Thanks!

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189

1 Answers1

-1

I think there is a simple way like this, hope it will help you

package main

import (
    "fmt"
    "strings"
    "text/scanner"
)

func isDigit(c byte) bool {
    if c >= 48 && c <= 57 {
        return true
    }
    return false
}

func main() {
    const src = `test123abc45alsdkfj`

    var s scanner.Scanner
    s.Init(strings.NewReader(src))

    for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {

        chars := s.TokenText()
        temp := string(chars[0])

        for i := range chars {
            if i > 0 {
                if isDigit(chars[i]) != isDigit(chars[i-1]) {
                    fmt.Println(temp)
                    temp = string(chars[i])
                } else {
                    temp += string(chars[i])
                }
            }
        }
    }

}

and output will be

test
123
abc
45
iamatsundere181
  • 1,401
  • 1
  • 16
  • 38