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

后端 未结 7 1678
有刺的猬
有刺的猬 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
    }
    
    0 讨论(0)
  • 2021-01-05 15:26

    If the string looks like whatever;START;extract;END;whatever you can use this which will get the string in between:

    // 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)
        if e == -1 {
            return
        }
        return str[s:e]
    }
    

    What happens here is it will find first index of START, adds length of START string and returns all that exists from there until first index of END.

    0 讨论(0)
  • 2021-01-05 15:27

    Read up on the strings package. Have a look into the SplitAfter function which can do something like this:

    var sample = "[this][is my][string]"
    t := strings.SplitAfter(sample, "[")
    

    That should produce a slice something like: "[", "this][", "is my][", "string]". Using further functions for Trimming you should get your solution. Best of luck.

    0 讨论(0)
  • 2021-01-05 15:30

    In the strings pkg you can use the Replacer to great affect.

    r := strings.NewReplacer("<h1>", "", "</h1>", "")
    fmt.Println(r.Replace("<h1>Hello World!</h1>"))
    

    Go play!

    0 讨论(0)
  • 2021-01-05 15:36

    There are lots of ways to split strings in all programming languages.

    Since I don't know what you are especially asking for I provide a sample way to get the output you want from your sample.

    package main
    
    import "strings"
    import "fmt"
    
    func main() {
        initial := "<h1>Hello World!</h1>"
    
        out := strings.TrimLeft(strings.TrimRight(initial,"</h1>"),"<h1>")
        fmt.Println(out)
    }
    

    In the above code you trim <h1> from the left of the string and </h1> from the right.

    As I said there are hundreds of ways to split specific strings and this is only a sample to get you started.

    Hope it helps, Good luck with Golang :)

    DB

    0 讨论(0)
  • 2021-01-05 15:36
    func Split(str, before, after string) string {
        a := strings.SplitAfterN(str, before, 2)
        b := strings.SplitAfterN(a[len(a)-1], after, 2)
        if 1 == len(b) {
            return b[0]
        }
        return b[0][0:len(b[0])-len(after)]
    }
    

    the first call of SplitAfterN will split the original string into array of 2 parts divided by the first found after string, or it will produce array containing 1 part equal to the original string.

    second call of SplitAfterN uses a[len(a)-1] as input, as it is "the last item of array a". so either string after after or the original string str. the input will be split into array of 2 parts divided by the first found before string, or it will produce array containing 1 part equal to the input.

    if after was not found than we can simply return b[0] as it is equal to a[len(a)-1]

    if after is found, it will be included at the end of b[0] string, therefore you have to trim it via b[0][0:len(b[0])-len(after)]

    all strings are case sensitive

    0 讨论(0)
提交回复
热议问题