import "github.com/mgutz/str"
Package str is a comprehensive set of string functions to build more Go
awesomeness. Str complements Go's standard packages and does not duplicate
functionality found in strings
or strconv
.
Str is based on plain functions instead of object-based methods, consistent with Go standard string packages.
str.Between("<a>foo</a>", "<a>", "</a>") == "foo"
Str supports pipelining instead of chaining
s := str.Pipe("\nabcdef\n", Clean, BetweenF("a", "f"), ChompLeftF("bc"))
User-defined filters can be added to the pipeline by inserting a function or closure that returns a function with this signature
func(string) string
- Variables
- [func Between](#func godoc between)
- func BetweenF
- func Camelize
- func Capitalize
- func CharAt
- func CharAtF
- func ChompLeft
- func ChompLeftF
- func ChompRight
- func ChompRightF
- func Classify
- func ClassifyF
- func Clean
- func Dasherize
- func DecodeHTMLEntities
- func EnsurePrefix
- func EnsurePrefixF
- func EnsureSuffix
- func EnsureSuffixF
- func EscapeHTML
- func Humanize
- func Iif
- func IndexOf
- func IsAlpha
- func IsAlphaNumeric
- func IsEmpty
- func IsLower
- func IsNumeric
- func IsUpper
- func Left
- func LeftF
- func LeftOf
- func Letters
- func Lines
- func Map
- func Match
- func Pad
- func PadF
- func PadLeft
- func PadLeftF
- func PadRight
- func PadRightF
- func Pipe
- func QuoteItems
- func ReplaceF
- func ReplacePattern
- func ReplacePatternF
- func Reverse
- func Right
- func RightF
- func RightOf
- func SetTemplateDelimiters
- func Slice
- func SliceContains
- func SliceF
- func SliceIndexOf
- func Slugify
- func StripPunctuation
- func StripTags
- func Substr
- func SubstrF
- func Template
- func TemplateDelimiters
- func TemplateWithDelimiters
- func ToArgv
- func ToBool
- func ToBoolOr
- func ToFloat32Or
- func ToFloat64Or
- func ToIntOr
- func Underscore
- func UnescapeHTML
- func WrapHTML
- func WrapHTMLF
var ToFloatOr = ToFloat64Or
ToFloatOr parses as a float64 or returns defaultValue.
var Verbose = false
Verbose flag enables console output for those functions that have counterparts in Go's excellent stadard packages.
func Between
func Between(s, left, right string) string
Between extracts a string between left and right strings.
func BetweenF
func BetweenF(left, right string) func(string) string
BetweenF is the filter form for Between.
func Camelize
func Camelize(s string) string
Camelize return new string which removes any underscores or dashes and convert a string into camel casing.
func Capitalize
func Capitalize(s string) string
Capitalize uppercases the first char of s and lowercases the rest.
func CharAt
func CharAt(s string, index int) string
CharAt returns a string from the character at the specified position.
func CharAtF
func CharAtF(index int) func(string) string
CharAtF is the filter form of CharAt.
func ChompLeft
func ChompLeft(s, prefix string) string
ChompLeft removes prefix at the start of a string.
func ChompLeftF
func ChompLeftF(prefix string) func(string) string
ChompLeftF is the filter form of ChompLeft.
func ChompRight
func ChompRight(s, suffix string) string
ChompRight removes suffix from end of s.
func ChompRightF
func ChompRightF(suffix string) func(string) string
ChompRightF is the filter form of ChompRight.
func Classify
func Classify(s string) string
Classify returns a camelized string with the first letter upper cased.
func ClassifyF
func ClassifyF(s string) func(string) string
ClassifyF is the filter form of Classify.
func Clean
func Clean(s string) string
Clean compresses all adjacent whitespace to a single space and trims s.
func Dasherize
func Dasherize(s string) string
Dasherize converts a camel cased string into a string delimited by dashes.
func DecodeHTMLEntities
func DecodeHTMLEntities(s string) string
DecodeHTMLEntities decodes HTML entities into their proper string representation. DecodeHTMLEntities is an alias for html.UnescapeString
func EnsurePrefix
func EnsurePrefix(s, prefix string) string
EnsurePrefix ensures s starts with prefix.
func EnsurePrefixF
func EnsurePrefixF(prefix string) func(string) string
EnsurePrefixF is the filter form of EnsurePrefix.
func EnsureSuffix
func EnsureSuffix(s, suffix string) string
EnsureSuffix ensures s ends with suffix.
func EnsureSuffixF
func EnsureSuffixF(suffix string) func(string) string
EnsureSuffixF is the filter form of EnsureSuffix.
func EscapeHTML
func EscapeHTML(s string) string
EscapeHTML is alias for html.EscapeString.
func Humanize
func Humanize(s string) string
Humanize transforms s into a human friendly form.
func Iif
func Iif(condition bool, truthy string, falsey string) string
Iif is short for immediate if. If condition is true return truthy else falsey.
func IndexOf
func IndexOf(s string, needle string, start int) int
IndexOf finds the index of needle in s starting from start.
func IsAlpha
func IsAlpha(s string) bool
IsAlpha returns true if a string contains only letters from ASCII (a-z,A-Z). Other letters from other languages are not supported.
func IsAlphaNumeric
func IsAlphaNumeric(s string) bool
IsAlphaNumeric returns true if a string contains letters and digits.
func IsEmpty
func IsEmpty(s string) bool
IsEmpty returns true if the string is solely composed of whitespace.
func IsLower
func IsLower(s string) bool
IsLower returns true if s comprised of all lower case characters.
func IsNumeric
func IsNumeric(s string) bool
IsNumeric returns true if a string contains only digits from 0-9. Other digits not in Latin (such as Arabic) are not currently supported.
func IsUpper
func IsUpper(s string) bool
IsUpper returns true if s contains all upper case chracters.
func Left
func Left(s string, n int) string
Left returns the left substring of length n.
func LeftF
func LeftF(n int) func(string) string
LeftF is the filter form of Left.
func LeftOf
func LeftOf(s string, needle string) string
LeftOf returns the substring left of needle.
func Letters
func Letters(s string) []string
Letters returns an array of runes as strings so it can be indexed into.
func Lines
func Lines(s string) []string
Lines convert windows newlines to unix newlines then convert to an Array of lines.
func Map
func Map(arr []string, iterator func(string) string) []string
Map maps an array's iitem through an iterator.
func Match
func Match(s, pattern string) bool
Match returns true if patterns matches the string
func Pad
func Pad(s, c string, n int) string
Pad pads string s on both sides with c until it has length of n.
func PadF
func PadF(c string, n int) func(string) string
PadF is the filter form of Pad.
func PadLeft
func PadLeft(s, c string, n int) string
PadLeft pads s on left side with c until it has length of n.
func PadLeftF
func PadLeftF(c string, n int) func(string) string
PadLeftF is the filter form of PadLeft.
func PadRight
func PadRight(s, c string, n int) string
PadRight pads s on right side with c until it has length of n.
func PadRightF
func PadRightF(c string, n int) func(string) string
PadRightF is the filter form of Padright
func Pipe
func Pipe(s string, funcs ...func(string) string) string
Pipe pipes s through one or more string filters.
func QuoteItems
func QuoteItems(arr []string) []string
QuoteItems quotes all items in array, mostly for debugging.
func ReplaceF
func ReplaceF(old, new string, n int) func(string) string
ReplaceF is the filter form of strings.Replace.
func ReplacePattern
func ReplacePattern(s, pattern, repl string) string
ReplacePattern replaces string with regexp string. ReplacePattern returns a copy of src, replacing matches of the Regexp with the replacement string repl. Inside repl, $ signs are interpreted as in Expand, so for instance $1 represents the text of the first submatch.
func ReplacePatternF
func ReplacePatternF(pattern, repl string) func(string) string
ReplacePatternF is the filter form of ReplaceRegexp.
func Reverse
func Reverse(s string) string
Reverse a string
func Right
func Right(s string, n int) string
Right returns the right substring of length n.
func RightF
func RightF(n int) func(string) string
RightF is the Filter version of Right.
func RightOf
func RightOf(s string, prefix string) string
RightOf returns the substring to the right of prefix.
func SetTemplateDelimiters(opening, closing string)
SetTemplateDelimiters sets the delimiters for Template function. Defaults to "{{" and "}}"
func Slice
func Slice(s string, start, end int) string
Slice slices a string. If end is negative then it is the from the end of the string.
func SliceContains
func SliceContains(slice []string, val string) bool
SliceContains determines whether val is an element in slice.
func SliceF
func SliceF(start, end int) func(string) string
SliceF is the filter for Slice.
func SliceIndexOf
func SliceIndexOf(slice []string, val string) int
SliceIndexOf gets the indx of val in slice. Returns -1 if not found.
func Slugify
func Slugify(s string) string
Slugify converts s into a dasherized string suitable for URL segment.
func StripPunctuation
func StripPunctuation(s string) string
StripPunctuation strips puncation from string.
func StripTags
func StripTags(s string, tags ...string) string
StripTags strips all of the html tags or tags specified by the parameters
func Substr
func Substr(s string, index int, n int) string
Substr returns a substring of s starting at index of length n.
func SubstrF
func SubstrF(index, n int) func(string) string
SubstrF is the filter form of Substr.
func Template
func Template(s string, values map[string]interface{}) string
Template is a string template which replaces template placeholders delimited by "{{" and "}}" with values from map. The global delimiters may be set with SetTemplateDelimiters.
func TemplateDelimiters
func TemplateDelimiters() (opening string, closing string)
TemplateDelimiters is the getter for the opening and closing delimiters for Template.
func TemplateWithDelimiters(s string, values map[string]interface{}, opening, closing string) string
TemplateWithDelimiters is string template with user-defineable opening and closing delimiters.
func ToArgv
func ToArgv(s string) []string
ToArgv converts string s into an argv for exec.
func ToBool
func ToBool(s string) bool
ToBool fuzzily converts truthy values.
func ToBoolOr
func ToBoolOr(s string, defaultValue bool) bool
ToBoolOr parses s as a bool or returns defaultValue.
func ToFloat32Or
func ToFloat32Or(s string, defaultValue float32) float32
ToFloat32Or parses as a float32 or returns defaultValue on error.
func ToFloat64Or
func ToFloat64Or(s string, defaultValue float64) float64
ToFloat64Or parses s as a float64 or returns defaultValue.
func ToIntOr
func ToIntOr(s string, defaultValue int) int
ToIntOr parses s as an int or returns defaultValue.
func Underscore
func Underscore(s string) string
Underscore returns converted camel cased string into a string delimited by underscores.
func UnescapeHTML
func UnescapeHTML(s string) string
UnescapeHTML is an alias for html.UnescapeString.
func WrapHTML
func WrapHTML(s string, tag string, attrs map[string]string) string
WrapHTML wraps s within HTML tag having attributes attrs. Note, WrapHTML does not escape s value.
func WrapHTMLF
func WrapHTMLF(tag string, attrs map[string]string) func(string) string
WrapHTMLF is the filter form of WrapHTML.