Skip to content

Instantly share code, notes, and snippets.

@flaviocopes
Last active June 23, 2024 09:05
Show Gist options
  • Save flaviocopes/6717857ca7ba7266d41330329eeb5ad0 to your computer and use it in GitHub Desktop.
Save flaviocopes/6717857ca7ba7266d41330329eeb5ad0 to your computer and use it in GitHub Desktop.
Go: check if a string starts with a substring #golang
package main
import (
"strings"
)
func main() {
strings.HasPrefix("foobar", "foo") // true
}
@proclaim
Copy link

proclaim commented Mar 8, 2019

Thanks!

@nexus166
Copy link

hey, newb here: how is this faster/more efficient than say

switch strings.ToUpper(str[:1]) {
    case "x":
        ..
    case "y":
        ..
}

?

Thanks and Regards!

@KSoto
Copy link

KSoto commented May 7, 2019

Thanks

@montaro
Copy link

montaro commented May 15, 2019

Thanks

@hanksudo
Copy link

Thanks

@tomocrafter
Copy link

Thanks

@aerostitch
Copy link

aerostitch commented Jun 30, 2019

@nexus166

hey, newb here: how is this faster/more efficient than say

switch strings.ToUpper(str[:1]) {
    case "x":
        ..
    case "y":
        ..
}

?

Thanks and Regards!

It's not case-insensitive but it does the same, here's the code: https://golang.org/src/strings/strings.go?s=11177:11214#L438

@vakenbolt
Copy link

Much obliged! 🙏

@koajca
Copy link

koajca commented Oct 18, 2019

Thanks

@goodstress
Copy link

Thanks

@PeterlitsZo
Copy link

so... how to let regex string match the begining?

@zplusfour
Copy link

thanks!

@lawliet-cpp
Copy link

Thanks!

@jonton11
Copy link

Thank you!

@ckcr4lyf
Copy link

thank

@spatecon
Copy link

Merci!

@StevenConradEllis
Copy link

Gracias!

@stephennancekivell
Copy link

Still going strong in 2022!

@NiklasZeroZero
Copy link

Danke

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment