从Go中的切片中删除字符串

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

Removing a string from a slice in Go

问题

我可以帮你翻译代码部分。以下是翻译好的代码:

我有一个字符串切片,我想要移除其中的一个特定字符串。

  1. strings := []string
  2. strings = append(strings, "one")
  3. strings = append(strings, "two")
  4. strings = append(strings, "three")

现在,我该如何从 strings 中移除字符串 "two"

英文:

I have a slice of strings, and I want to remove a specific one.

  1. strings := []string
  2. strings = append(strings, "one")
  3. strings = append(strings, "two")
  4. strings = append(strings, "three")

Now how can I remove the string "two" from strings?

答案1

得分: 24

找到你想要删除的元素,并像删除任何其他切片中的元素一样将其删除。

找到它是一个线性搜索。删除可以使用以下其中一种切片技巧来完成:

  1. a = append(a[:i], a[i+1:]...)
  2. // 或者
  3. a = a[:i+copy(a[i:], a[i+1:])]

以下是完整的解决方案(在Go Playground上尝试):

  1. s := []string{"one", "two", "three"}
  2. // 查找并删除"two"
  3. for i, v := range s {
  4. if v == "two" {
  5. s = append(s[:i], s[i+1:]...)
  6. break
  7. }
  8. }
  9. fmt.Println(s) // 输出 [one three]

如果你想将其封装成一个函数:

  1. func remove(s []string, r string) []string {
  2. for i, v := range s {
  3. if v == r {
  4. return append(s[:i], s[i+1:]...)
  5. }
  6. }
  7. return s
  8. }

使用它:

  1. s := []string{"one", "two", "three"}
  2. s = remove(s, "two")
  3. fmt.Println(s) // 输出 [one three]
英文:

Find the element you want to remove and remove it like you would any element from any other slice.

Finding it is a linear search. Removing is one of the following slice tricks:

  1. a = append(a[:i], a[i+1:]...)
  2. // or
  3. a = a[:i+copy(a[i:], a[i+1:])]

Here is the complete solution (try it on the Go Playground):

  1. s := []string{"one", "two", "three"}
  2. // Find and remove "two"
  3. for i, v := range s {
  4. if v == "two" {
  5. s = append(s[:i], s[i+1:]...)
  6. break
  7. }
  8. }
  9. fmt.Println(s) // Prints [one three]

If you want to wrap it into a function:

  1. func remove(s []string, r string) []string {
  2. for i, v := range s {
  3. if v == r {
  4. return append(s[:i], s[i+1:]...)
  5. }
  6. }
  7. return s
  8. }

Using it:

  1. s := []string{"one", "two", "three"}
  2. s = remove(s, "two")
  3. fmt.Println(s) // Prints [one three]

答案2

得分: 1

这是一个用于删除特定索引处元素的函数:

  1. package main
  2. import "fmt"
  3. import "errors"
  4. func main() {
  5. strings := []string{}
  6. strings = append(strings, "one")
  7. strings = append(strings, "two")
  8. strings = append(strings, "three")
  9. strings, err := remove(strings, 1)
  10. if err != nil {
  11. fmt.Println("Something went wrong: ", err)
  12. } else {
  13. fmt.Println(strings)
  14. }
  15. }
  16. func remove(s []string, index int) ([]string, error) {
  17. if index >= len(s) {
  18. return nil, errors.New("Out of Range Error")
  19. }
  20. return append(s[:index], s[index+1:]...), nil
  21. }

Go Playground上尝试一下。

英文:

Here is a function to remove the element at a particular index:

  1. package main
  2. import "fmt"
  3. import "errors"
  4. func main() {
  5. strings := []string{}
  6. strings = append(strings, "one")
  7. strings = append(strings, "two")
  8. strings = append(strings, "three")
  9. strings, err := remove(strings, 1)
  10. if err != nil {
  11. fmt.Println("Something went wrong : ", err)
  12. } else {
  13. fmt.Println(strings)
  14. }
  15. }
  16. func remove(s []string, index int) ([]string, error) {
  17. if index >= len(s) {
  18. return nil, errors.New("Out of Range Error")
  19. }
  20. return append(s[:index], s[index+1:]...), nil
  21. }

Try it on Go Playground

huangapple
  • 本文由 发表于 2015年12月3日 23:52:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/34070369.html
匿名

发表评论

匿名网友

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

确定