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

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

Removing a string from a slice in Go

问题

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

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

strings := []string
strings = append(strings, "one")
strings = append(strings, "two")
strings = append(strings, "three")

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

英文:

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

strings := []string
strings = append(strings, "one")
strings = append(strings, "two")
strings = append(strings, "three")

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

答案1

得分: 24

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

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

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

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

s := []string{"one", "two", "three"}

// 查找并删除"two"
for i, v := range s {
    if v == "two" {
        s = append(s[:i], s[i+1:]...)
        break
    }
}

fmt.Println(s) // 输出 [one three]

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

func remove(s []string, r string) []string {
    for i, v := range s {
        if v == r {
            return append(s[:i], s[i+1:]...)
        }
    }
    return s
}

使用它:

s := []string{"one", "two", "three"}
s = remove(s, "two")
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:

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

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

s := []string{"one", "two", "three"}

// Find and remove "two"
for i, v := range s {
	if v == "two" {
		s = append(s[:i], s[i+1:]...)
		break
	}
}

fmt.Println(s) // Prints [one three]

If you want to wrap it into a function:

func remove(s []string, r string) []string {
	for i, v := range s {
		if v == r {
			return append(s[:i], s[i+1:]...)
		}
	}
	return s
}

Using it:

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

答案2

得分: 1

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

package main

import "fmt"
import "errors"

func main() {
    strings := []string{}
    strings = append(strings, "one")
    strings = append(strings, "two")
    strings = append(strings, "three")
    strings, err := remove(strings, 1)
    if err != nil {
        fmt.Println("Something went wrong: ", err)
    } else {
        fmt.Println(strings)
    }
}

func remove(s []string, index int) ([]string, error) {
    if index >= len(s) {
        return nil, errors.New("Out of Range Error")
    }
    return append(s[:index], s[index+1:]...), nil
}

Go Playground上尝试一下。

英文:

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

package main

import "fmt"
import "errors"

func main() {
	strings := []string{}
	strings = append(strings, "one")
	strings = append(strings, "two")
	strings = append(strings, "three")
	strings, err := remove(strings, 1)
	if err != nil {
		fmt.Println("Something went wrong : ", err)
	} else {
		fmt.Println(strings)
	}

}

func remove(s []string, index int) ([]string, error) {
	if index >= len(s) {
		return nil, errors.New("Out of Range Error")
	}
	return append(s[:index], s[index+1:]...), nil
}

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:

确定