Documentation
¶
Overview ¶
Package stringsp is a plus to the standard "strings" package.
Index ¶
- func CallbackFields(s string, prepare func(n int), newField func(f string))
- func CallbackFieldsFunc(s string, isSpace func(rune) bool, prepare func(n int), ...)
- func Compare(a, b string) int
- func FullJoin(a []string, prefix, sep, suffix string) string
- func Get(s *string) string
- func IndentLinesExceptFirst(s, indent string) string
- func LessFunc(l []string) func(i, j int) bool
- func MatchPrefix(s, prefix string) (string, bool)
- func SliceInsert(s []string, index int, e ...string) []string
- func SliceRemove(s []string, index int) []string
- type Set
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CallbackFields ¶
CallbackFields calls CallbackFieldsFunc with unicode.IsSpace.
Example ¶
CallbackFields("Hello World ", func(n int) { fmtp.Printfln("Totally %d lines:", n) }, func(f string) { fmtp.Printfln(" %s", f) })
Output: Totally 2 lines: Hello World
func CallbackFieldsFunc ¶
func CallbackFieldsFunc(s string, isSpace func(rune) bool, prepare func(n int), newField func(f string))
CallbackFieldsFunc is similar to strings.FieldsFunc but results are returned by two callback functions. prepare is firstly called with the number of total fields. Then each field is given by calling newField in order. If no fields are found, prepare is called with a zero value.
This function is especially useful when we want to split a string into a slice of named types other than strings or into a non-slice data structure at all.
func FullJoin ¶
FullJoin is similar to strings.Join with additional prefix/suffix if a is non-empty.
Example ¶
a := []string{ "item1", "item two", } fmt.Println(FullJoin(a, "(", "), (", ")"))
Output: (item1), (item two)
func Get ¶
Get safely returns the pointed contents of a string pionter. Returns "" for a nil pointer.
func IndentLinesExceptFirst ¶
IndentLinesExceptFirst appends a leading indent to each lines of the string except the first line.
func MatchPrefix ¶
MatchPrefix checks whether the specified string has a prefix, if so a string removing the prefix is returned; otherwise the original string is returned
func SliceInsert ¶
SliceInsert inserts some elements at index-th position and resturns the result slice.
func SliceRemove ¶
SliceRemove removes the index-th element from the slice and returns the result slice.
Types ¶
type Set ¶
type Set map[string]struct{}
Set is a set of strings
func NewSet ¶
NeSet creates a string set with specified elements. You need to use this function only when you want to create a Set with intial elements. Different from a normal map, a nil value of Set is ok to add elemnents by calling the Add method.
func (Set) Elements ¶
Elements returns all elements in the set as a string slice. NOTE the order of elements may change even if the set does not change.