Documentation
¶
Overview ¶
Package strutil gets your string manipulation covered! Features include escaping reserved words, wildcard compare, and pattern extraction.
Index ¶
- func After(s, substr string) string
- func Before(s, substr string) string
- func Between(s string, head, tail []string, first int) []string
- func Escape(text, reserved, subst string) string
- func Prefix(s string, length int) string
- func Random(n int, charset ...string) string
- func RandomBase36(n int) string
- func RandomBase62(n int) string
- func RandomBase64(n int) string
- func RandomHex(n int) string
- func RemoveEmpty(s *[]string)
- func Secret(n int, charset ...string) (string, error)
- func Select(s *[]string, f func(string) bool)
- func Suffix(s string, length int) string
- func Unescape(text, reserved, subst string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
After returns all characters after the the specified substring
Example ¶
package main import ( "fmt" "github.com/gozl/strutil" ) func main() { src := "baby123" after1 := strutil.After(src, "1") after2 := strutil.After(src, "?") after3 := strutil.After(src, "by") after4 := strutil.After(src, "") after5 := strutil.After("", "xxx") fmt.Println("-->" + after1 + "<--") fmt.Println("-->" + after2 + "<--") fmt.Println("-->" + after3 + "<--") fmt.Println("-->" + after4 + "<--") fmt.Println("-->" + after5 + "<--") }
Output: -->23<-- -->baby123<-- -->123<-- -->baby123<-- --><--
func Before ¶
Before returns all characters before the the specified substring
Example ¶
package main import ( "fmt" "github.com/gozl/strutil" ) func main() { src := "baby123" before1 := strutil.Before(src, "1") before2 := strutil.Before(src, "?") before3 := strutil.Before(src, "b") before4 := strutil.Before(src, "") before5 := strutil.Before("", "xxx") fmt.Println("-->" + before1 + "<--") fmt.Println("-->" + before2 + "<--") fmt.Println("-->" + before3 + "<--") fmt.Println("-->" + before4 + "<--") fmt.Println("-->" + before5 + "<--") }
Output: -->baby<-- -->baby123<-- --><-- -->baby123<-- --><--
func Between ¶
Between returns all substrings sandwiched between the specified head and tail
Example ¶
package main import ( "fmt" "github.com/gozl/strutil" ) func main() { src := "we have <tag1> and <tag2> to extract" extracted := strutil.Between(src, []string{"<"}, []string{">"}, -1) for _, q := range extracted { fmt.Println("extracted text: " + q) } src2 := "substring inside $ and @ and ++123#more between and stuff @blahblah blah" extracted2 := strutil.Between(src2, []string{"$", "@", "++"}, []string{"#", "@"}, -1) for _, q := range extracted2 { fmt.Println("extracted2 text: " + q) } }
Output: extracted text: tag1 extracted text: tag2 extracted2 text: 123
func Escape ¶
Escape replaces all occurances of reserved with substitute in text, but makes sure that any substitute already in text can be recovered using Unescape().
Example ¶
package main import ( "fmt" "github.com/gozl/strutil" ) func main() { src := "a %reserved% message for john & cage llc" escaped := strutil.Escape(src, "%", "&") recovered := strutil.Unescape(escaped, "%", "&") fmt.Println("Original text: " + src) fmt.Println("Escaped text: " + escaped) fmt.Println("Recovered text: " + recovered) }
Output: Original text: a %reserved% message for john & cage llc Escaped text: a &1reserved&1 message for john &2 cage llc Recovered text: a %reserved% message for john & cage llc
Example (Subst) ¶
package main import ( "fmt" "github.com/gozl/strutil" ) func main() { src := "a %reserved% message for john &22 &2 cage llc" escaped := strutil.Escape(src, "%", "&") recovered := strutil.Unescape(escaped, "%", "&") fmt.Println("Original text: " + src) fmt.Println("Escaped text: " + escaped) fmt.Println("Recovered text: " + recovered) }
Output: Original text: a %reserved% message for john &22 &2 cage llc Escaped text: a &1reserved&1 message for john &222 &22 cage llc Recovered text: a %reserved% message for john &22 &2 cage llc
func Prefix ¶
Prefix returns the specified number of characters from the start of a string. If the number of characters specified is more than the length of the string, the entire string is returned. If the length is negative, characters are returned from the end of the string.
Example ¶
package main import ( "fmt" "github.com/gozl/strutil" ) func main() { src := "123baby" prefix1 := strutil.Prefix(src, 3) prefix2 := strutil.Prefix(src, 300) prefix3 := strutil.Prefix(src, -3) fmt.Println("prefix1: " + prefix1) fmt.Println("prefix2: " + prefix2) fmt.Println("prefix3: " + prefix3) }
Output: prefix1: 123 prefix2: 123baby prefix3: aby
func Random ¶
Random returns a random string of the specified length. Uses alphanumeric characters by default.
func RandomBase36 ¶
RandomBase36 returns a random string of the specified length in the character range 0-9a-z
func RandomBase62 ¶
RandomBase62 returns a random string of the specified length in the character range 0-9a-zA-Z
func RandomBase64 ¶
RandomBase64 returns a random string of the specified length in the character range 0-9a-zA-Z+/
func RandomHex ¶
RandomHex returns a random string of the specified length in the character range 0-9a-f
func RemoveEmpty ¶
func RemoveEmpty(s *[]string)
RemoveEmpty removes empty strings from a string slice
Example ¶
package main import ( "fmt" "github.com/gozl/strutil" ) func main() { src := []string{"fish", "cow", "bird", "", "banana", "", "coffee", ""} strutil.RemoveEmpty(&src) for _, q := range src { fmt.Println("item: " + q) } }
Output: item: fish item: cow item: bird item: banana item: coffee
func Secret ¶
Secret generates a secure random string using crypto/rand. Possible characters are `abcdefghinpqrt`, `ABCDEFGHJKLMNPRSTUWXY`, 0-9, and the symbols `~@_/+:`. Password policy compliance is not handled by this function. Error may be returned due to crypto/rand Read().
func Select ¶
Select removes strings fror a string slice that does not pass a selector function
Example ¶
package main import ( "fmt" "github.com/gozl/strutil" ) func main() { src := []string{"fish", "cow", "bird", "", "banana", "", "coffee", ""} strutil.Select(&src, func(s string) bool { return len(s) > 4 }) for _, q := range src { fmt.Println("item: " + q) } }
Output: item: banana item: coffee
func Suffix ¶
Suffix returns the specified number of characters from the end of a string. If the number of characters specified is more than the length of the string, the entire string is returned. If the length is negative, characters are returned from the start of the string.
Example ¶
package main import ( "fmt" "github.com/gozl/strutil" ) func main() { src := "baby123" suffix1 := strutil.Suffix(src, 3) suffix2 := strutil.Suffix(src, 300) suffix3 := strutil.Suffix(src, -3) fmt.Println("suffix1: " + suffix1) fmt.Println("suffix2: " + suffix2) fmt.Println("suffix3: " + suffix3) }
Output: suffix1: 123 suffix2: baby123 suffix3: bab
Types ¶
This section is empty.