如何在Golang中删除字符串的最后一个字符?

huangapple go评论88阅读模式
英文:

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

问题

我想要移除字符串的最后一个字符,但在这之前,我想要检查最后一个字符是否为“+”。这该如何实现?

英文:

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?

答案1

得分: 72

以下是翻译好的部分:

这里有几种去除尾部加号的方法。

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)

	// 去除一个尾部的 '+'
	s1 := s
	if last := len(s1) - 1; last >= 0 && s1[last] == '+' {
		s1 = s1[:last]
	}
	fmt.Println("s1:", s1)

	// 去除所有尾部的 '+'
	s2 := s
	s2 = strings.TrimRight(s2, "+")
	fmt.Println("s2:", s2)

	// 去除后缀 '+'
	s3 := s
	s3 = TrimSuffix(s3, "+")
	fmt.Println("s3:", s3)
}

输出:

s:  a string ++
s1: a string +
s2: a string 
s3: a string +
英文:

Here are several ways to remove trailing plus sign(s).

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 +

答案2

得分: 40

根据@KarthikGR的评论,添加了以下示例:

https://play.golang.org/p/ekDeT02ZXoq

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.TrimSuffix("Foo++", "+"))
}

返回:

Foo+
英文:

Based on the comment of @KarthikGR the following example was added:

https://play.golang.org/p/ekDeT02ZXoq

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.TrimSuffix("Foo++", "+"))
}

returns:

Foo+

答案3

得分: 21

没有内置的方法。但是手动操作很简单。

s := "mystring+"
sz := len(s)

if sz > 0 && s[sz-1] == '+' {
    s = s[:sz-1]
}
英文:

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]
}

答案4

得分: 1

package main

import (
"fmt"
)

func main() {

s := "venga|ese|sabor|"

newString := ""
if len(s) > 0 {
	newString = s[:len(s)-1]
}

fmt.Println(newString)

}

output: venga|ese|sabor

go playground:
https://go.dev/play/p/o9ExIEuU0SF

英文:
package main

import (
	"fmt"
)

func main() {

	s := "venga|ese|sabor|"

	newString := ""
	if len(s) > 0 {
		newString = s[:len(s)-1]
	}

	fmt.Println(newString)
}

output: venga|ese|sabor

go playground:
https://go.dev/play/p/o9ExIEuU0SF

答案5

得分: -1

一个简单的符合UTF规范的字符串修剪器是

string([]rune(foo)[:len(foo)-1]))

所以我会选择

f2 := []rune(foo)
for f2[len(f2)-1] == '+' {
    f2 = f2[:len(f2)-1]
}
foo = string(f2)

https://go.dev/play/p/anOwXlfQWaF

我不确定为什么其他答案会以它们的方式修剪,因为它们是按字节修剪的。

英文:

@llazzaro

A simple UTF compliant string trimmer is

string([]rune(foo)[:len(foo)-1]))

So I'd go with

f2 := []rune(foo)
for f2[len(f2)-1] == '+'{
    f2 = f2[:len(f2)-1]
}
foo = string(f2)

https://go.dev/play/p/anOwXlfQWaF

I'm not sure why the other answers trimmed the way that they do, because they're trimming bytes.

huangapple
  • 本文由 发表于 2012年1月1日 01:13:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/8689425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定