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

后端 未结 4 976
时光说笑
时光说笑 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:14

    No builtin way. But it's trivial to do manually.

    s := "mystring+"
    sz := len(s)
    
    if sz > 0 && s[sz-1] == '+' {
        s = s[:sz-1]
    }
    

提交回复
热议问题