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

后端 未结 7 1743
有刺的猬
有刺的猬 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: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 := "

    Hello World!

    " out := strings.TrimLeft(strings.TrimRight(initial,""),"

    ") fmt.Println(out) }

    In the above code you trim

    from the left of the string and

    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

提交回复
热议问题