Is this Scanner bug in Swift?

36 views Asked by At

I have a sample code using Scanner class.
I expected that resultString has " testing & testing1&testing2"
However, resultString is "testing &testing1&testing2"

When scanUpToString() function called, I guess the scanning string is trimmed.
Is this bug? or not?

let testString = " testing & testing1&testing2"
let scanner = Scanner(string: testString)
var resultString: String = ""

while scanner.isAtEnd == false {
    if let beforeString = scanner.scanUpToString("&") {
        resultString += beforeString
    }
    
    guard scanner.isAtEnd == false else {
        break
    }
    
    if let scanResult = scanner.scanString("&") {
        resultString += "&"
    }
}
0

There are 0 answers