在 Go 语言中切片比数组更强大、灵活、方便,是一种轻量级的数据结构。切片是一个可变长度的序列,用于存储相似类型的元素,不允许在同一个切片中存储不同类型的元素。
在 Go 切片中,您可以借助SearchStrings()函数在给定的字符串切片中搜索字符串类型的元素。此函数在已排序的字符串切片中搜索给定元素,如果存在于给定切片中,则返回该元素的索引。如果给定的元素在切片中不可用(它可能是len(s_slice) ),则它返回索引以在切片中插入元素。指定的切片必须按升序排序。它是在 sort 包下定义的,因此您必须在程序中导入 sort 包才能访问 SearchStrings函数。
句法:
func SearchStrings(s_slice []string, s string64) int
示例 1:
// Go program to illustrate how to search an
// element of string type in the slice of strings
package main
import (
"fmt"
"sort"
)
// Main function
func main() {
// Creating and initializing
// slice of strings
// Using shorthand declaration
slice_1 := []string{"C", "Go", "Java", "C#", "Ruby"}
slice_2 := []string{"GEEKs", "123geeks", "gfg", "GeeksforGeeks"}
var f1, f2, f3 string
f1 = "GEEKs"
f2 = "C"
f3 = "gfg"
// Sorting the given
// slice of strings
sort.Strings(slice_1)
sort.Strings(slice_2)
// Displaying the slices
fmt.Println("Slice 1: ", slice_1)
fmt.Println("Slice 2: ", slice_2)
// Searching a int type element
// in the given slice
// Using SearchStrings function
res1 := sort.SearchStrings(slice_1, f1)
res2 := sort.SearchStrings(slice_2, f2)
res3 := sort.SearchStrings(slice_2, f3)
// Displaying the results
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2: ", res2)
fmt.Println("Result 3: ", res3)
}
输出:
Slice 1: [C C# Go Java Ruby]
Slice 2: [123geeks GEEKs GeeksforGeeks gfg]
Result 1: 2
Result 2: 1
Result 3: 3
示例 2:
// Go program to illustrate how to search an element
// of string type in the slice of strings
package main
import (
"fmt"
"sort"
)
// Main function
func main() {
// Creating and searching an element
// in the given slice of strings
// Using SearchStrings function
res1 := sort.SearchStrings([]string{"apple", "banana",
"kiwi", "orange"}, "kiwi")
res2 := sort.SearchStrings([]string{"Cat", "Cow",
"Dog", "Parrot"}, "Cat")
// Displaying the results
fmt.Println("Result 1: ", res1)
fmt.Println("Result 2: ", res2)
}
输出:
Result 1: 2
Result 2: 0