Skip to content

Instantly share code, notes, and snippets.

@alexvlasov
Forked from yuchan/substring.go
Created June 7, 2018 13:07
Show Gist options
  • Save alexvlasov/bd8f91e872876172c61de3e85c5f1c94 to your computer and use it in GitHub Desktop.
Save alexvlasov/bd8f91e872876172c61de3e85c5f1c94 to your computer and use it in GitHub Desktop.
Substring #GoLang
//Original
func substr(s string,pos,length int) string{
bytes:=[]byte(s)
l := pos+length
if l > len(bytes) {
l = len(bytes)
}
return string(bytes[pos:l])
}
//Simply
s[pos:length]
//[]byte -> []rune
//Correct version
func substr(s string,pos,length int) string{
runes:=[]rune(s)
l := pos+length
if l > len(runes) {
l = len(runes)
}
return string(runes[pos:l])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment