How to remove the last character of a string in Golang?

后端 未结 4 980
时光说笑
时光说笑 2021-02-03 17:43

I want to remove the very last character of a string, but before I do so I want to check if the last character is a \"+\". How can this be done?

4条回答
  •  爱一瞬间的悲伤
    2021-02-03 18:01

    Based on the answer of @KarthikGR the following example was added:

    https://play.golang.org/p/ekDeT02ZXoq

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        fmt.Println(strings.TrimSuffix("Foo++", "+"))
    }
    

    returns:

    Foo+
    

提交回复
热议问题