当前位置:网站首页>Golang中删除字符串的最后一个字符
Golang中删除字符串的最后一个字符
2022-07-03 08:04:00 【Bel_Ami同学】
package main
import (
"fmt"
"strings"
)
func TrimSuffix(s, suffix string) string {
if strings.HasSuffix(s, suffix) {
s = s[:len(s)-len(suffix)]
}
return s
}
func main() {
s := "a string ++"
fmt.Println("s: ", s)
// Trim one trailing '+'.
s1 := s
if last := len(s1) - 1; last >= 0 && s1[last] == '+' {
s1 = s1[:last]
}
fmt.Println("s1:", s1)
// Trim all trailing '+'.
s2 := s
s2 = strings.TrimRight(s2, "+")
fmt.Println("s2:", s2)
// Trim suffix "+".
s3 := s
s3 = TrimSuffix(s3, "+")
fmt.Println("s3:", s3)
}
输出:
s: a string ++
s1: a string +
s2: a string
s3: a string +
边栏推荐
- regular expression
- Storage of data
- Install cross compiler arm none liunx gnueabihf
- C语言-入门-精华版-带你走进编程(一)
- 多旅行商问题——公式和求解过程概述
- WorldView卫星遥感影像数据/米级分辨率遥感影像
- Usage of (case, when) in PostgreSQL
- JS common basic case sorting (continuous update)
- Wpf: solve the problem that materialdesign:dialoghost cannot be closed
- MaxCompute字符串分割函数-SPLIT_PART
猜你喜欢

超限黑客认知

Editor Extensions

the installer has encountered an unexpected error installing this package
![[MySQL 12] MySQL 8.0.18 reinitialization](/img/e1/9874df18bbc8d80c3c5c5fe39aefc9.png)
[MySQL 12] MySQL 8.0.18 reinitialization

Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does

Iterm2 setting

Install cross compiler arm none liunx gnueabihf

创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03

Technical dry goods | some thoughts on the future of AI architecture

【cocos creator】点击按钮切换界面
随机推荐
[step on the pit series] MySQL failed to modify the root password
An intern's journey to cnosdb
使用 FileChannel 进行文件的复制拷贝
Oracle insert single quotation mark
C language learning notes (mind map)
Redis profile
unity2019_ Input management
How to clear the console password for s7700 device
Compilation error: "not in executable format: file format not recognized"“
Unity performance optimization
Screenshot tool snipaste
Retail philosophy retail psychological warfare after reading -- 7-11 is a good product!
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
Maxcompute string splitting function -split_ PART
Iterm2 setting
什么是定义?什么是声明?它们有何区别?
多旅行商问题——公式和求解过程概述
Redis batch startup and shutdown script
Youyou1 of xlua knapsack system
P2622 light off problem II (state compression search)