Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Go: check if a string starts with a substring #golang
package main
import (
"strings"
)
func main() {
strings.HasPrefix("foobar", "foo") // true
}
@DomHudson

This comment has been minimized.

Copy link

@DomHudson DomHudson commented Aug 16, 2018

Thanks

@WestleyK

This comment has been minimized.

Copy link

@WestleyK WestleyK commented Nov 14, 2018

👍 Thanks!

@coarsehorse

This comment has been minimized.

Copy link

@coarsehorse coarsehorse commented Nov 25, 2018

Nice!

@proclaim

This comment has been minimized.

Copy link

@proclaim proclaim commented Mar 8, 2019

Thanks!

@nexus166

This comment has been minimized.

Copy link

@nexus166 nexus166 commented Mar 14, 2019

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

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

?

Thanks and Regards!

@KSoto

This comment has been minimized.

Copy link

@KSoto KSoto commented May 7, 2019

Thanks

@montaro

This comment has been minimized.

Copy link

@montaro montaro commented May 15, 2019

Thanks

@hanksudo

This comment has been minimized.

Copy link

@hanksudo hanksudo commented May 28, 2019

Thanks

@tomocrafter

This comment has been minimized.

Copy link

@tomocrafter tomocrafter commented Jun 10, 2019

Thanks

@aerostitch

This comment has been minimized.

Copy link

@aerostitch 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

This comment has been minimized.

Copy link

@vakenbolt vakenbolt commented Sep 25, 2019

Much obliged! 🙏

@koajca

This comment has been minimized.

Copy link

@koajca koajca commented Oct 18, 2019

Thanks

@viewratio

This comment has been minimized.

Copy link

@viewratio viewratio commented Dec 7, 2019

Thanks

@PeterlitsZo

This comment has been minimized.

Copy link

@PeterlitsZo PeterlitsZo commented Mar 26, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.