Created
June 2, 2012 09:20
-
-
Save yuchan/2857491 to your computer and use it in GitHub Desktop.
Substring #GoLang
This file contains hidden or 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