Last active
July 16, 2018 14:14
-
-
Save NaniteFactory/b80fda552643728b91a27e252595c4f1 to your computer and use it in GitHub Desktop.
GetStringInBetween() with a bug fix
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
| package main | |
| import ( | |
| "fmt" | |
| "strings" | |
| ) | |
| func main() { | |
| str := []byte("sdfsdffio jeo <!--foo wij ewie jo fwiejwoeifjwoeifjoweifjo --> weifj ow ejowifj woesdfsdffio jeo <!--bar wij ewie jo fwiejwo2222222222eifjwoeifjoweifjo --> weifj ow ejowifj woe<!--bars -->") | |
| fmt.Println(GetStringInBetween(string(str), "<!--foo ", " -->")) | |
| fmt.Println(GetStringInBetween(string(str), "<!--bars ", " -->")) | |
| fmt.Println(GetStringInBetween(string(str), "<!--bar ", " -->")) | |
| /* | |
| wij ewie jo fwiejwoeifjwoeifjoweifjo | |
| wij ewie jo fwiejwo2222222222eifjwoeifjoweifjo | |
| */ | |
| } | |
| // GetStringInBetween Returns empty string if no start string found | |
| func GetStringInBetween(str string, start string, end string) (result string) { | |
| s := strings.Index(str, start) | |
| if s == -1 { | |
| return | |
| } | |
| s += len(start) | |
| e := strings.Index(str[s:], end) + s // fix | |
| return str[s:e] | |
| } | |
| // ref) | |
| // https://stackoverflow.com/questions/26916952/go-retrieve-a-string-from-between-two-characters-or-other-strings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment