-
-
Save alexvlasov/bd8f91e872876172c61de3e85c5f1c94 to your computer and use it in GitHub Desktop.
Substring #GoLang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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