当前位置:网站首页>Delete the last character of the string in golang
Delete the last character of the string in golang
2022-07-03 08:13:00 【Bel_ AMI classmate】
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)
}
Output :
s: a string ++
s1: a string +
s2: a string
s3: a string +
边栏推荐
猜你喜欢
C language learning notes (mind map)
Basic operation and process control 2
Wpf: solve the problem that materialdesign:dialoghost cannot be closed
L'installateur a été installé avec une erreur inattendue
WorldView卫星遥感影像数据/米级分辨率遥感影像
VMware virtual machine configuration static IP
Oracle insert single quotation mark
Unity change default editor
Haproxy+kept cluster setup 02
C#课程设计之学生教务管理系统
随机推荐
tslib库的移植
PostGIS space function
Minimap plug-in
How to establish rectangular coordinate system in space
Client server model
How does yarn link help developers debug NPM packages?
I want to do large screen data visualization application feature analysis
Clip Related Script
Lua framwrok framework starts
Luaframwrok handles resource updates
C language learning notes (mind map)
【cocos creator】点击按钮切换界面
About Wireshark's unsuccessful installation of npcap
Three characteristics
LinkList
swagger文档配置
[cocos creator] Click the button to switch the interface
Zohocrm deluge function application time verification
Storage of data
十六进制编码简介