当前位置:网站首页>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 +
边栏推荐
- I want to do large screen data visualization application feature analysis
- [untitled]
- 一个实习生的CnosDB之旅
- Maxcompute string splitting function -split_ PART
- Quality blog——
- Mutual call between Lua and C #
- [at] abc 258G - Triangle 三元組可達-暴力
- Redis view client connection
- [at] ABC 258g - triple Reach - violence
- Ventuz Foundation Series "one step at the door"
猜你喜欢
Getting started with minicom
Pulitzer Prize in the field of information graphics - malofiej Award
Unity2019_ Natural ambient light_ Sky box
Basic operation and process control
WPF:解决MaterialDesign:DialogHost 无法关闭问题
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
C language learning notes (mind map)
C language - Introduction - essence Edition - take you into programming (I)
数据的存储
一个实习生的CnosDB之旅
随机推荐
Unity one click AssetBundle
方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
RM delete file
Unity change default editor
链式长取值
Wechat applet taro learning record
Unity2019_ Natural ambient light_ Sky box
JS to implement publish and subscribe
[USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
Yolo series --- xml2txt script
P2704 [NOI2001] 炮兵阵地(状压dp)
I want to do large screen data visualization application feature analysis
Docker installs MySQL and successfully uses Navicat connection
unity2019_ Input management
About Wireshark's unsuccessful installation of npcap
Install cross compiler arm none liunx gnueabihf
P1896 [SCOI2005] 互不侵犯(状压dp)
haproxy+keepalived搭建01
什么是定义?什么是声明?它们有何区别?
Clip Related Script