Documentation
¶
Overview ¶
Various operations for string slices for Go
Package strarr is a collection of functions to manipulate string arrays/slices. Some functions were adapted from the strings package to work with string slices, other were ported from PHP 'array_*' function equivalents.
Example (Basic) ¶
This example shows basic usage of various functions by manipulating the array 'arr'.
arr := []string{"Go", "nuts", "for", "Go"} foo := strarr.Repeat("Go", 3) fmt.Println(foo) fmt.Println(strarr.Count(arr, "Go")) fmt.Println(strarr.Index(arr, "Go")) fmt.Println(strarr.LastIndex(arr, "Go")) if strarr.Contains(arr, "nuts") { arr = strarr.Replace(arr, []string{"Insanely"}) } fmt.Println(arr) str := strarr.Shift(&arr) fmt.Println(str) fmt.Println(arr) strarr.Unshift(&arr, "Really") fmt.Println(arr) fmt.Println(strarr.ToUpper(arr)) fmt.Println(strarr.ToLower(arr)) fmt.Println(strarr.ToTitle(arr)) fmt.Println(strarr.Trim(arr, "Really")) fmt.Println(strarr.Filter(arr, "Go")) fmt.Println(strarr.Diff(arr, foo)) fmt.Println(strarr.Intersect(arr, foo)) // this will return 2 random elements from arr // but it will fail testing, so... // fmt.Println(strarr.Rand(arr,2)) fmt.Println(strarr.Reverse(arr))
Output: [Go Go Go] 2 0 3 [Insanely nuts for Go] Insanely [nuts for Go] [Really nuts for Go] [REALLY NUTS FOR GO] [really nuts for go] [Really Nuts For Go] [nuts for Go] [Go] [Really nuts for] [Go] [Go for nuts Really]
Index ¶
- func Contains(a []string, s string) bool
- func ContainsPrefix(a []string, prefix string) bool
- func ContainsSuffix(a []string, suffix string) bool
- func Count(a []string, s string) int
- func Diff(a, b []string) []string
- func Fill(n int, s string) []string
- func Filter(a []string, s string) []string
- func FilterFunc(a []string, s string, f func(string, string) bool) []string
- func FilterPrefix(a []string, prefix string) []string
- func FilterSuffix(a []string, suffix string) []string
- func Index(a []string, s string) int
- func IndexPrefix(a []string, prefix string) int
- func IndexSuffix(a []string, suffix string) int
- func Intersect(a, b []string) []string
- func LastIndex(a []string, s string) int
- func LastIndexPrefix(a []string, prefix string) int
- func LastIndexSuffix(a []string, suffix string) int
- func LastSearch(a []string, s string) int
- func Map(mapping func(string) string, a []string) []string
- func Pop(a *[]string) string
- func Push(a *[]string, s ...string) int
- func Rand(a []string, n int) []string
- func Repeat(s string, n int) []string
- func Replace(a, b []string) []string
- func Reverse(a []string) []string
- func Search(a []string, s string) int
- func Shift(a *[]string) string
- func Shuffle(a []string) []string
- func ToLower(a []string) []string
- func ToTitle(a []string) []string
- func ToUpper(a []string) []string
- func Trim(a []string, s string) []string
- func TrimFunc(a []string, s string, f func(string, string) bool) []string
- func TrimPrefix(a []string, prefix string) []string
- func TrimSuffix(a []string, suffix string) []string
- func Unshift(a *[]string, s ...string) int
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContainsPrefix ¶
ContainsPrefix returns true if any entry in 'a' has prefix 'prefix', false otherwise
func ContainsSuffix ¶
ContainsSuffix returns true if any entry in 'a' has suffix 'suffix', false otherwise
func FilterFunc ¶
FilterFunc returns a slice with all the entries of 'a' that match string 's' using a callback function 'f' Callback is f(value, key string) where value is in 'a' and is checked for key. If true, value will be filtered.
func FilterPrefix ¶
FilterPrefix returns a slice with all the entries of 'a' that have prefix 'prefix'
func FilterSuffix ¶
FilterSuffix returns a slice with all the entries of 'a' that have suffix 'suffix'
func IndexPrefix ¶
IndexPrefix returns the index of the first entry in 'a' with prefix 'prefix', or -1 if not found
func IndexSuffix ¶
IndexSuffix returns the index of the first entry in 'a' with suffix 'suffix', or -1 if not found
func LastIndexPrefix ¶
LastIndexPrefix returns the index of the last entry in 'a' with prefix 'prefix', or -1 if not found
func LastIndexSuffix ¶
LastIndexSuffix returns the index of the last entry in 'a' with suffix 'suffix', or -1 if not found
func LastSearch ¶
LastSearch returns the index of the last entry containing the substring 's' in 'a', or -1 if not found
func Pop ¶
Pop removes the last element in '*a' and returns it, shortening the array by one. If '*a' is empty returns empty string "". Note that this function will change the array pointed by 'a'.
func Push ¶
Push appends one or more elements to '*a' and returns the number of entries. Note that this function will change the array pointed by 'a'.
func Replace ¶
Replace returns a slice with the values of 'a' replaced with the index-matching values of 'b'. If 'b' has more entries than 'a' they will be appended.
func Search ¶
Search returns the index of the first entry containing the substring 's' in 'a', or -1 if not found
func Shift ¶
Shift shifts the first element of '*a' and returns it, shortening the array by one. If '*a' is empty returns empty string "". Note that this function will change the array pointed by 'a'.
func TrimFunc ¶
TrimFunc returns a slice with all the entries of 'a' that don't match string 's' using a callback function 'f' Callback is f(value, key string) where value is in 'a' and is checked for key. If true, value will be trimmed.
func TrimPrefix ¶
TrimPrefix returns a slice with all the entries of 'a' that don't have prefix 'prefix'
func TrimSuffix ¶
TrimSuffix returns a slice with all the entries of 'a' that don't have suffix 'suffix'
Types ¶
This section is empty.