当前位置:网站首页>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 +
边栏推荐
- C language - Introduction - essence Edition - take you into programming (I)
- PHP common sorting algorithm
- [USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
- [step on the pit series] MySQL failed to modify the root password
- 一个实习生的CnosDB之旅
- 璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
- Static keyword
- Quality blog——
- C language learning notes (mind map)
- Wechat applet taro learning record
猜你喜欢

My touch screen production "brief history" 1

Redis batch startup and shutdown script

An intern's journey to cnosdb

一条通往服务器所有端口的隧道
![[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December](/img/51/81ceaf8746ec7455ea8abf9f038e81.jpg)
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December

STM32F103 SPI (pit Diary)

freetype库的移植

Unity performance optimization

IP production stream is so close to me

C语言-入门-精华版-带你走进编程(一)
随机推荐
Huawei s5700 switch initialization and configuration SSH and telnet remote login methods
Lua framwrok framework starts
Haproxy+kept cluster setup 02
tslib库的移植
haproxy+keepalived搭建01
How to configure GDAL under idea
Use filechannel to copy files
JS to implement publish and subscribe
idea取消引用顯示效果
Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire
The general trend of data news releases the power of visual reporting ----- essays after reading
使用 FileChannel 进行文件的复制拷贝
PIP uses image website to solve the problem of slow network speed
Transfinite hacker cognition
Compilation error: "not in executable format: file format not recognized"“
Mutual call between Lua and C #
the installer has encountered an unexpected error installing this package
My touch screen production "brief history" 2
Zohocrm deluge function application time verification
oracle 插入单引号