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

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

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

以下是翻译好的部分:

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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func TrimSuffix(s, suffix string) string {
  7. if strings.HasSuffix(s, suffix) {
  8. s = s[:len(s)-len(suffix)]
  9. }
  10. return s
  11. }
  12. func main() {
  13. s := "a string ++"
  14. fmt.Println("s: ", s)
  15. // 去除一个尾部的 '+'
  16. s1 := s
  17. if last := len(s1) - 1; last >= 0 && s1[last] == '+' {
  18. s1 = s1[:last]
  19. }
  20. fmt.Println("s1:", s1)
  21. // 去除所有尾部的 '+'
  22. s2 := s
  23. s2 = strings.TrimRight(s2, "+")
  24. fmt.Println("s2:", s2)
  25. // 去除后缀 '+'
  26. s3 := s
  27. s3 = TrimSuffix(s3, "+")
  28. fmt.Println("s3:", s3)
  29. }

输出:

  1. s: a string ++
  2. s1: a string +
  3. s2: a string
  4. s3: a string +
英文:

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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func TrimSuffix(s, suffix string) string {
  7. if strings.HasSuffix(s, suffix) {
  8. s = s[:len(s)-len(suffix)]
  9. }
  10. return s
  11. }
  12. func main() {
  13. s := "a string ++"
  14. fmt.Println("s: ", s)
  15. // Trim one trailing '+'.
  16. s1 := s
  17. if last := len(s1) - 1; last >= 0 && s1[last] == '+' {
  18. s1 = s1[:last]
  19. }
  20. fmt.Println("s1:", s1)
  21. // Trim all trailing '+'.
  22. s2 := s
  23. s2 = strings.TrimRight(s2, "+")
  24. fmt.Println("s2:", s2)
  25. // Trim suffix "+".
  26. s3 := s
  27. s3 = TrimSuffix(s3, "+")
  28. fmt.Println("s3:", s3)
  29. }

Output:

  1. s: a string ++
  2. s1: a string +
  3. s2: a string
  4. s3: a string +

答案2

得分: 40

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. fmt.Println(strings.TrimSuffix("Foo++", "+"))
  8. }

返回:

  1. Foo+
英文:

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

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

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. func main() {
  7. fmt.Println(strings.TrimSuffix("Foo++", "+"))
  8. }

returns:

  1. Foo+

答案3

得分: 21

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

  1. s := "mystring+"
  2. sz := len(s)
  3. if sz > 0 && s[sz-1] == '+' {
  4. s = s[:sz-1]
  5. }
英文:

No builtin way. But it's trivial to do manually.

  1. s := "mystring+"
  2. sz := len(s)
  3. if sz > 0 && s[sz-1] == '+' {
  4. s = s[:sz-1]
  5. }

答案4

得分: 1

package main

import (
"fmt"
)

func main() {

  1. s := "venga|ese|sabor|"
  2. newString := ""
  3. if len(s) > 0 {
  4. newString = s[:len(s)-1]
  5. }
  6. fmt.Println(newString)

}

output: venga|ese|sabor

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

英文:
  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. s := "venga|ese|sabor|"
  7. newString := ""
  8. if len(s) > 0 {
  9. newString = s[:len(s)-1]
  10. }
  11. fmt.Println(newString)
  12. }

output: venga|ese|sabor

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

答案5

得分: -1

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

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

所以我会选择

  1. f2 := []rune(foo)
  2. for f2[len(f2)-1] == '+' {
  3. f2 = f2[:len(f2)-1]
  4. }
  5. foo = string(f2)

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

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

英文:

@llazzaro

A simple UTF compliant string trimmer is

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

So I'd go with

  1. f2 := []rune(foo)
  2. for f2[len(f2)-1] == '+'{
  3. f2 = f2[:len(f2)-1]
  4. }
  5. 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:

确定