Archie
Archie

Reputation: 13

SplitN backwards?

I have a string that will always start with a sentence that well change each time but end with time then date. For example "I went to the shop 12:00 12/12/12" or "I like toast 11:20 13/10/14".

I want to extract the time out of these strings. Is it possible to do SplitN starting from the right?

Upvotes: 1

Views: 1134

Answers (1)

squiguy
squiguy

Reputation: 33370

If you know that it will always end with time and then date why not just go backwards with split?

Playground link

package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "I went to the shop 12:00 12/12/12"
    chunks := strings.Split(s, " ")
    time := chunks[len(chunks)-2]
    fmt.Println(time)
}

Upvotes: 1

Related Questions