Go: Retrieve a string from between two characters or other strings

后端 未结 7 1682
有刺的猬
有刺的猬 2021-01-05 14:47

Let\'s say for example that I have one string, like this:

Hello World!

What Go code would be able to extract Hel

7条回答
  •  走了就别回头了
    2021-01-05 15:24

    I improved the Jan Kardaš`s answer. now you can find string with more than 1 character at the start and end.

    func GetStringInBetweenTwoString(str string, startS string, endS string) (result string,found bool) {
        s := strings.Index(str, startS)
        if s == -1 {
            return result,false
        }
        newS := str[s+len(startS):]
        e := strings.Index(newS, endS)
        if e == -1 {
            return result,false
        }
        result = newS[:e]
        return result,true
    }
    

提交回复
热议问题