str

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2015 License: MIT Imports: 8 Imported by: 2

README

str

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
Index
Variables
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
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
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.

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

View Source
var ToFloatOr = ToFloat64Or

ToFloatOr parses as a float64 or returns defaultValue.

View Source
var Verbose = false

Verbose flag enables console output for those functions that have counterparts in Go's excellent stadard packages.

Functions

func Between

func Between(s, left, right string) string

Between extracts a string between left and right strings.

Example
eg(1, Between("<a>foo</a>", "<a>", "</a>"))
eg(2, Between("<a>foo</a></a>", "<a>", "</a>"))
eg(3, Between("<a><a>foo</a></a>", "<a>", "</a>"))
eg(4, Between("<a><a>foo</a></a>", "<a>", "</a>"))
eg(5, Between("<a>foo", "<a>", "</a>"))
eg(6, Between("Some strings } are very {weird}, dont you think?", "{", "}"))
eg(7, Between("This is ateststring", "", "test"))
eg(8, Between("This is ateststring", "test", ""))
Output:

1: foo
2: foo
3: <a>foo
4: <a>foo
5:
6: weird
7: This is a
8: string

func BetweenF

func BetweenF(left, right string) func(string) string

BetweenF is the filter form for Between.

Example
eg(1, Pipe("abc", BetweenF("a", "c")))
Output:

1: b

func Camelize

func Camelize(s string) string

Camelize return new string which removes any underscores or dashes and convert a string into camel casing.

Example
eg(1, Camelize("data_rate"))
eg(2, Camelize("background-color"))
eg(3, Camelize("-moz-something"))
eg(4, Camelize("_car_speed_"))
eg(5, Camelize("yes_we_can"))
Output:

1: dataRate
2: backgroundColor
3: MozSomething
4: CarSpeed
5: yesWeCan

func Capitalize

func Capitalize(s string) string

Capitalize uppercases the first char of s and lowercases the rest.

Example
eg(1, Capitalize("abc"))
eg(2, Capitalize("ABC"))
Output:

1: Abc
2: Abc

func CharAt

func CharAt(s string, index int) string

CharAt returns a string from the character at the specified position.

Example
eg(1, CharAt("abc", 1))
eg(2, CharAt("", -1))
eg(3, CharAt("", 0))
eg(4, CharAt("", 10))
eg(5, CharAt("abc", -1))
eg(6, CharAt("abc", 10))
Output:

1: b
2:
3:
4:
5:
6:

func CharAtF

func CharAtF(index int) func(string) string

CharAtF is the filter form of CharAt.

Example
eg(1, Pipe("abc", CharAtF(1)))
Output:

1: b

func ChompLeft

func ChompLeft(s, prefix string) string

ChompLeft removes prefix at the start of a string.

Example
eg(1, ChompLeft("foobar", "foo"))
eg(2, ChompLeft("foobar", "bar"))
eg(3, ChompLeft("", "foo"))
eg(4, ChompLeft("", ""))
eg(5, ChompLeft("foo", ""))
Output:

1: bar
2: foobar
3:
4:
5: foo

func ChompLeftF

func ChompLeftF(prefix string) func(string) string

ChompLeftF is the filter form of ChompLeft.

Example
eg(1, Pipe("abc", ChompLeftF("ab")))
Output:

1: c

func ChompRight

func ChompRight(s, suffix string) string

ChompRight removes suffix from end of s.

Example
eg(1, ChompRight("foobar", "foo"))
eg(2, ChompRight("foobar", "bar"))
eg(3, ChompRight("", "foo"))
eg(4, ChompRight("", ""))
Output:

1: foobar
2: foo
3:
4:

func ChompRightF

func ChompRightF(suffix string) func(string) string

ChompRightF is the filter form of ChompRight.

Example
eg(1, Pipe("abc", ChompRightF("bc")))
Output:

1: a

func Classify

func Classify(s string) string

Classify returns a camelized string with the first letter upper cased.

Example
eg(1, Classify("data_rate"))
eg(2, Classify("background-color"))
eg(3, Classify("-moz-something"))
eg(4, Classify("_car_speed_"))
eg(5, Classify("yes_we_can"))
Output:

1: DataRate
2: BackgroundColor
3: MozSomething
4: CarSpeed
5: YesWeCan

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.

Example
eg(1, Clean("clean"))
eg(2, Clean(""))
eg(3, Clean(" please\t    clean \t \n  me "))
Output:

1: clean
2:
3: please clean me

func Dasherize

func Dasherize(s string) string

Dasherize converts a camel cased string into a string delimited by dashes.

Example
eg(1, Dasherize("dataRate"))
eg(2, Dasherize("CarSpeed"))
eg(3, Dasherize("yesWeCan"))
eg(4, Dasherize(""))
eg(5, Dasherize("ABC"))
Output:

1: data-rate
2: -car-speed
3: yes-we-can
4:
5: -a-b-c

func DecodeHTMLEntities

func DecodeHTMLEntities(s string) string

DecodeHTMLEntities decodes HTML entities into their proper string representation. DecodeHTMLEntities is an alias for html.UnescapeString

Example
eg(1, DecodeHTMLEntities("Ken Thompson &amp; Dennis Ritchie"))
eg(2, DecodeHTMLEntities("3 &lt; 4"))
eg(3, DecodeHTMLEntities("http:&#47;&#47;"))
Output:

1: Ken Thompson & Dennis Ritchie
2: 3 < 4
3: http://

func EnsurePrefix

func EnsurePrefix(s, prefix string) string

EnsurePrefix ensures s starts with prefix.

Example
eg(1, EnsurePrefix("foobar", "foo"))
eg(2, EnsurePrefix("bar", "foo"))
eg(3, EnsurePrefix("", ""))
eg(4, EnsurePrefix("foo", ""))
eg(5, EnsurePrefix("", "foo"))
Output:

1: foobar
2: foobar
3:
4: foo
5: foo

func EnsurePrefixF

func EnsurePrefixF(prefix string) func(string) string

EnsurePrefixF is the filter form of EnsurePrefix.

Example
eg(1, Pipe("dir", EnsurePrefixF("./")))
Output:

1: ./dir

func EnsureSuffix

func EnsureSuffix(s, suffix string) string

EnsureSuffix ensures s ends with suffix.

Example
eg(1, EnsureSuffix("foobar", "bar"))
eg(2, EnsureSuffix("foo", "bar"))
eg(3, EnsureSuffix("", ""))
eg(4, EnsureSuffix("foo", ""))
eg(5, EnsureSuffix("", "bar"))
Output:

1: foobar
2: foobar
3:
4: foo
5: bar

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.

Example
eg(1, Humanize("the_humanize_string_method"))
eg(2, Humanize("ThehumanizeStringMethod"))
eg(3, Humanize("the humanize string method"))
Output:

1: The humanize string method
2: Thehumanize string method
3: The humanize string method

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.

Example
eg(1, Iif(true, "T", "F"))
eg(2, Iif(false, "T", "F"))
Output:

1: T
2: F

func IndexOf

func IndexOf(s string, needle string, start int) int

IndexOf finds the index of needle in s starting from start.

Example
eg(1, IndexOf("abcdef", "a", 0))
eg(2, IndexOf("abcdef", "a", 3))
eg(3, IndexOf("abcdef", "a", -2))
eg(4, IndexOf("abcdef", "a", 10))
eg(5, IndexOf("", "a", 0))
eg(6, IndexOf("abcdef", "", 2))
eg(7, IndexOf("abcdef", "", 1000))
Output:

1: 0
2: -1
3: -1
4: -1
5: -1
6: 2
7: 6

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.

Example
eg(1, IsAlpha("afaf"))
eg(2, IsAlpha("FJslfjkasfs"))
eg(3, IsAlpha("áéúóúÁÉÍÓÚãõÃÕàèìòùÀÈÌÒÙâêîôûÂÊÎÔÛäëïöüÄËÏÖÜçÇ"))
eg(4, IsAlpha("adflj43faljsdf"))
eg(5, IsAlpha("33"))
eg(6, IsAlpha("TT....TTTafafetstYY"))
eg(7, IsAlpha("-áéúóúÁÉÍÓÚãõÃÕàèìòùÀÈÌÒÙâêîôûÂÊÎÔÛäëïöüÄËÏÖÜçÇ"))
Output:

1: true
2: true
3: true
4: false
5: false
6: false
7: false

func IsAlphaNumeric

func IsAlphaNumeric(s string) bool

IsAlphaNumeric returns true if a string contains letters and digits.

Example
eg(1, IsAlphaNumeric("afaf35353afaf"))
eg(2, IsAlphaNumeric("FFFF99fff"))
eg(3, IsAlphaNumeric("99"))
eg(4, IsAlphaNumeric("afff"))
eg(5, IsAlphaNumeric("Infinity"))
eg(6, IsAlphaNumeric("áéúóúÁÉÍÓÚãõÃÕàèìòùÀÈÌÒÙâêîôûÂÊÎÔÛäëïöüÄËÏÖÜçÇ1234567890"))
eg(7, IsAlphaNumeric("-Infinity"))
eg(8, IsAlphaNumeric("-33"))
eg(9, IsAlphaNumeric("aaff.."))
eg(10, IsAlphaNumeric(".áéúóúÁÉÍÓÚãõÃÕàèìòùÀÈÌÒÙâêîôûÂÊÎÔÛäëïöüÄËÏÖÜçÇ1234567890"))
Output:

1: true
2: true
3: true
4: true
5: true
6: true
7: false
8: false
9: false
10: false

func IsEmpty

func IsEmpty(s string) bool

IsEmpty returns true if the string is solely composed of whitespace.

Example
eg(1, IsEmpty(" "))
eg(2, IsEmpty("\t\t\t   "))
eg(3, IsEmpty("\t\n "))
eg(4, IsEmpty("hi"))
Output:

1: true
2: true
3: true
4: false

func IsLower

func IsLower(s string) bool

IsLower returns true if s comprised of all lower case characters.

Example
eg(1, IsLower("a"))
eg(2, IsLower("A"))
eg(3, IsLower("abc"))
eg(4, IsLower("aBc"))
eg(5, IsLower("áéúóúãõàèìòùâêîôûäëïöüç"))
eg(6, IsLower("hi jp"))
eg(7, IsLower("ÁÉÍÓÚÃÕÀÈÌÒÙÂÊÎÔÛÄËÏÖÜÇ"))
eg(8, IsLower("áéúóúãõàèìòùâêîôûäëïöüçÁ"))
eg(9, IsLower("áéúóúãõàèìòùâêîôû äëïöüç"))
Output:

1: true
2: false
3: true
4: false
5: true
6: false
7: false
8: false
9: false

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.

Example
eg(1, IsNumeric("3"))
eg(2, IsNumeric("34.22"))
eg(3, IsNumeric("-22.33"))
eg(4, IsNumeric("NaN"))
eg(5, IsNumeric("Infinity"))
eg(6, IsNumeric("-Infinity"))
eg(7, IsNumeric("JP"))
eg(8, IsNumeric("-5"))
eg(9, IsNumeric("00099242424"))
Output:

1: true
2: false
3: false
4: false
5: false
6: false
7: false
8: false
9: true

func IsUpper

func IsUpper(s string) bool

IsUpper returns true if s contains all upper case chracters.

Example
eg(1, IsUpper("a"))
eg(2, IsUpper("A"))
eg(3, IsUpper("ABC"))
eg(4, IsUpper("aBc"))
eg(5, IsUpper("áéúóúãõàèìòùâêîôûäëïöüç"))
eg(6, IsUpper("HI JP"))
eg(7, IsUpper("ÁÉÍÓÚÃÕÀÈÌÒÙÂÊÎÔÛÄËÏÖÜÇ"))
eg(8, IsUpper("áéúóúãõàèìòùâêîôûäëïöüçÁ"))
eg(9, IsUpper("ÁÉÍÓÚÃÕÀÈÌÒÙÂÊÎ ÔÛÄËÏÖÜÇ"))
Output:

1: false
2: true
3: true
4: false
5: false
6: false
7: true
8: false
9: false

func Left

func Left(s string, n int) string

Left returns the left substring of length n.

Example
eg(1, Left("abcdef", 0))
eg(2, Left("abcdef", 1))
eg(3, Left("abcdef", 4))
eg(4, Left("abcdef", -2))
Output:

1:
2: a
3: abcd
4: ef

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.

Example
eg(1, LeftOf("abcdef", "def"))
eg(2, LeftOf("abcdef", "abc"))
eg(3, LeftOf("abcdef", ""))
eg(4, LeftOf("", "abc"))
eg(5, LeftOf("abcdef", "xyz"))
Output:

1: abc
2:
3: abcdef
4:
5:

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.

Example
eg(1, Lines("a\r\nb\nc\r\n"))
eg(2, Lines("a\r\nb\nc\r\nd"))
Output:

1: [a b c ]
2: [a b c d]

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

Example
eg(1, Match("foobar", `^fo.*r$`))
eg(2, Match("foobar", `^fo.*x$`))
eg(3, Match("", `^fo.*x$`))
Output:

1: true
2: false
3: false

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.

Example
eg(1, Pad("hello", "x", 5))
eg(2, Pad("hello", "x", 10))
eg(3, Pad("hello", "x", 11))
eg(4, Pad("hello", "x", 6))
eg(5, Pad("hello", "x", 1))
Output:

1: hello
2: xxxhelloxx
3: xxxhelloxxx
4: xhello
5: hello

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.

Example
eg(1, PadLeft("hello", "x", 5))
eg(2, PadLeft("hello", "x", 10))
eg(3, PadLeft("hello", "x", 11))
eg(4, PadLeft("hello", "x", 6))
eg(5, PadLeft("hello", "x", 1))
Output:

1: hello
2: xxxxxhello
3: xxxxxxhello
4: xhello
5: hello

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.

Example
eg(1, PadRight("hello", "x", 5))
eg(2, PadRight("hello", "x", 10))
eg(3, PadRight("hello", "x", 11))
eg(4, PadRight("hello", "x", 6))
eg(5, PadRight("hello", "x", 1))
Output:

1: hello
2: helloxxxxx
3: helloxxxxxx
4: hellox
5: hello

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.

Example
eg(1, Pipe("\nabcdef   \n", Clean, BetweenF("a", "f"), ChompLeftF("bc")))
Output:

1: de

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.

Example
eg(1, Pipe("abcdefab", ReplaceF("ab", "x", -1)))
eg(2, Pipe("abcdefab", ReplaceF("ab", "x", 1)))
eg(3, Pipe("abcdefab", ReplaceF("ab", "x", 0)))
Output:

1: xcdefx
2: xcdefab
3: abcdefab

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.

Example
eg(1, ReplacePattern("aabbcc", `a`, "x"))
Output:

1: xxbbcc

func ReplacePatternF

func ReplacePatternF(pattern, repl string) func(string) string

ReplacePatternF is the filter form of ReplaceRegexp.

Example
eg(1, Pipe("aabbcc", ReplacePatternF(`a`, "x")))
Output:

1: xxbbcc

func Reverse

func Reverse(s string) string

Reverse a string

Example
eg(1, Reverse("abc"))
eg(2, Reverse("中文"))
Output:

1: cba
2: 文中
func Right(s string, n int) string

Right returns the right substring of length n.

Example
eg(1, Right("abcdef", 0))
eg(2, Right("abcdef", 1))
eg(3, Right("abcdef", 4))
eg(4, Right("abcdef", -2))
Output:

1:
2: f
3: cdef
4: ab

func RightF

func RightF(n int) func(string) string

RightF is the Filter version of Right.

Example
eg(1, Pipe("abcdef", RightF(3)))
Output:

1: def

func RightOf

func RightOf(s string, prefix string) string

RightOf returns the substring to the right of prefix.

Example
eg(1, RightOf("abcdef", "abc"))
eg(2, RightOf("abcdef", "def"))
eg(3, RightOf("abcdef", ""))
eg(4, RightOf("", "abc"))
eg(5, RightOf("abcdef", "xyz"))
Output:

1: def
2:
3: abcdef
4:
5:

func SetTemplateDelimiters

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 added in v1.2.0

func SliceContains(slice []string, val string) bool

SliceContains determines whether val is an element in slice.

Example
eg(1, SliceContains([]string{"foo", "bar"}, "foo"))
eg(2, SliceContains(nil, "foo"))
eg(3, SliceContains([]string{"foo", "bar"}, "bah"))
eg(4, SliceContains([]string{"foo", "bar"}, ""))
Output:

1: true
2: false
3: false
4: false

func SliceF

func SliceF(start, end int) func(string) string

SliceF is the filter for Slice.

func SliceIndexOf added in v1.2.0

func SliceIndexOf(slice []string, val string) int

SliceIndexOf gets the indx of val in slice. Returns -1 if not found.

Example
eg(1, SliceIndexOf([]string{"foo", "bar"}, "foo"))
eg(2, SliceIndexOf(nil, "foo"))
eg(3, SliceIndexOf([]string{"foo", "bar"}, "bah"))
eg(4, SliceIndexOf([]string{"foo", "bar"}, ""))
eg(5, SliceIndexOf([]string{"foo", "bar"}, "bar"))
Output:

1: 0
2: -1
3: -1
4: -1
5: 1

func Slugify

func Slugify(s string) string

Slugify converts s into a dasherized string suitable for URL segment.

Example
eg(1, Slugify("foo bar"))
eg(2, Slugify("foo/bar bah"))
eg(3, Slugify("foo-bar--bah"))
Output:

1: foo-bar
2: foobar-bah
3: foo-bar-bah

func StripPunctuation

func StripPunctuation(s string) string

StripPunctuation strips puncation from string.

Example
eg(1, StripPunctuation("My, st[ring] *full* of %punct)"))
Output:

1: My string full of punct

func StripTags

func StripTags(s string, tags ...string) string

StripTags strips all of the html tags or tags specified by the parameters

Example
eg(1, StripTags("<p>just <b>some</b> text</p>"))
eg(2, StripTags("<p>just <b>some</b> text</p>", "p"))
eg(3, StripTags("<a><p>just <b>some</b> text</p></a>", "a", "p"))
eg(4, StripTags("<a><p>just <b>some</b> text</p></a>", "b"))
Output:

1: just some text
2: just <b>some</b> text
3: just <b>some</b> text
4: <a><p>just some text</p></a>

func Substr

func Substr(s string, index int, n int) string

Substr returns a substring of s starting at index of length n.

Example
eg(1, Substr("abcdef", 2, -1))
eg(2, Substr("abcdef", 2, 0))
eg(3, Substr("abcdef", 2, 1))
eg(4, Substr("abcdef", 2, 3))
eg(5, Substr("abcdef", 2, 4))
eg(6, Substr("abcdef", 2, 100))
eg(7, Substr("abcdef", 0, 1))
Output:

1:
2:
3: c
4: cde
5: cdef
6: cdef
7: a

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.

Example
eg(1, Template("Hello {{name}} at {{date-year}}", map[string]interface{}{"name": "foo", "date-year": 2014}))
eg(2, Template("Hello {{name}}", map[string]interface{}{"name": ""}))
SetTemplateDelimiters("{", "}")
eg(3, Template("Hello {name} at {date-year}", map[string]interface{}{"name": "foo", "date-year": 2014}))
Output:

1: Hello foo at 2014
2: Hello
3: Hello foo at 2014

func TemplateDelimiters

func TemplateDelimiters() (opening string, closing string)

TemplateDelimiters is the getter for the opening and closing delimiters for Template.

func TemplateWithDelimiters

func TemplateWithDelimiters(s string, values map[string]interface{}, opening, closing string) string

TemplateWithDelimiters is string template with user-defineable opening and closing delimiters.

Example
eg(1, TemplateWithDelimiters("Hello {{name}} at {{date-year}}", map[string]interface{}{"name": "foo", "date-year": 2014}, "{{", "}}"))
eg(2, TemplateWithDelimiters("Hello #{name} at #{date-year}", map[string]interface{}{"name": "foo", "date-year": 2014}, "#{", "}"))
eg(3, TemplateWithDelimiters("Hello (name) at (date-year)", map[string]interface{}{"name": "foo", "date-year": 2014}, "(", ")"))
eg(4, TemplateWithDelimiters("Hello [name] at [date-year]", map[string]interface{}{"name": "foo", "date-year": 2014}, "[", "]"))
eg(5, TemplateWithDelimiters("Hello *name* at *date-year*", map[string]interface{}{"name": "foo", "date-year": 2014}, "*", "*"))
eg(6, TemplateWithDelimiters("Hello $name$ at $date-year$", map[string]interface{}{"name": "foo", "date-year": 2014}, "$", "$"))
Output:

1: Hello foo at 2014
2: Hello foo at 2014
3: Hello foo at 2014
4: Hello foo at 2014
5: Hello foo at 2014
6: Hello foo at 2014

func ToArgv

func ToArgv(s string) []string

ToArgv converts string s into an argv for exec.

Example
eg(1, QuoteItems(ToArgv(`GO_ENV=test gosu --watch foo@release "some quoted string 'inside'"`)))
eg(2, QuoteItems(ToArgv(`gosu foo\ bar`)))
eg(3, QuoteItems(ToArgv(`gosu --test="some arg" -w -s a=123`)))
Output:

1: ["GO_ENV=test" "gosu" "--watch" "foo@release" "some quoted string 'inside'"]
2: ["gosu" "foo bar"]
3: ["gosu" "--test=some arg" "-w" "-s" "a=123"]

func ToBool

func ToBool(s string) bool

ToBool fuzzily converts truthy values.

Example
eg(1, ToBool("true"))
eg(2, ToBool("yes"))
eg(3, ToBool("1"))
eg(4, ToBool("on"))
eg(5, ToBool("false"))
eg(6, ToBool("no"))
eg(7, ToBool("0"))
eg(8, ToBool("off"))
eg(9, ToBool(""))
eg(10, ToBool("?"))
Output:

1: true
2: true
3: true
4: true
5: false
6: false
7: false
8: false
9: false
10: false

func ToBoolOr

func ToBoolOr(s string, defaultValue bool) bool

ToBoolOr parses s as a bool or returns defaultValue.

Example
eg(1, ToBoolOr("foo", true))
eg(2, ToBoolOr("foo", false))
eg(3, ToBoolOr("true", false))
eg(4, ToBoolOr("", true))
Output:

1: true
2: false
3: true
4: true

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.

Example
eg(1, ToIntOr("foo", 0))
eg(2, ToIntOr("", 1))
eg(3, ToIntOr("100", 0))
eg(4, ToIntOr("-1", 1))
Output:

1: 0
2: 1
3: 100
4: -1

func Underscore

func Underscore(s string) string

Underscore returns converted camel cased string into a string delimited by underscores.

Example
eg(1, Underscore("fooBar"))
eg(2, Underscore("FooBar"))
eg(3, Underscore(""))
eg(4, Underscore("x"))
Output:

1: foo_bar
2: _foo_bar
3:
4: x

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.

Example
eg(1, WrapHTML("foo", "span", nil))
eg(2, WrapHTML("foo", "", nil))
eg(3, WrapHTML("foo", "", map[string]string{"class": "bar"}))
Output:

1: <span>foo</span>
2: <div>foo</div>
3: <div class="bar">foo</div>

func WrapHTMLF

func WrapHTMLF(tag string, attrs map[string]string) func(string) string

WrapHTMLF is the filter form of WrapHTML.

Example
eg(1, Pipe("foo", WrapHTMLF("div", nil)))
Output:

1: <div>foo</div>

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL