Documentation ¶
Overview ¶
Package strings operations on string: 1.IsBlank/IsNotBlank/IsAnyBlank/IsNoneBlank - checks if a String contains text 2.Trim/TrimLeft/TrimRight/TrimSpace - removes leading and trailing whitespace 3.Index/IndexLast/IndexLastStart/IndexLastOrdinal/IndexStart/IndexOrdinal - index checks 4.Contains/ContainsAny/ContainsRune/ContainsSpace - does String contains only/none/any of these characters 5.EqualFold - compares two strings 6.Slice/Left/Right/Mid/Before/After/BeforeLast/AfterLast/Between/BetweenSame - substring extractions 7.Split/SplitAfter/SplitAfterN/SplitN - splits a String into an array of substrings 8.Remove/RemoveSpace/RemoveStart/RemoveEnd - removes part of a string 9.Repeat/Pad/PadStart/PadEnd/PadLeft/PadRight/Center - pads a string 10.ToLower/ToUpper/Title/ToTitle/Camel/UnCamel - changes the case of a string 11.Abbr/Anon/Anonymity/Anonymous - abbreviates a string using ellipsis or asterisk
Index ¶
- func Abbr(s string, maxWidth int) string
- func After(s, sep string) string
- func AfterLast(s, sep string) string
- func Anon(s string) string
- func Anonymity(s string) string
- func Anonymous(s string, leftLen, rightLen, starLen int) string
- func Before(s, sep string) string
- func BeforeLast(s, sep string) string
- func Between(s, start, end string) string
- func BetweenSame(s, tag string) string
- func Betweens(s, start, end string) (out []string)
- func Camel(s string) string
- func Center(in string, size int, sep string) (out string)
- func Contains(s, substr string) bool
- func ContainsAny(s, chars string) bool
- func ContainsNone(s, chars string) bool
- func ContainsOnly(s, chars string) bool
- func ContainsRune(s string, r rune) bool
- func ContainsSlice(s string, chars []string) bool
- func ContainsSliceAny(s string, chars []string) bool
- func ContainsSpace(s string) bool
- func Count(s, sep string) int
- func EqualFold(s, t string) bool
- func Fields(s string) []string
- func FieldsFunc(s string, f func(rune) bool) []string
- func FieldsSpace(s string) []string
- func HasAnyPrefix(s string, prefix ...string) (string, bool)
- func HasAnySuffix(s string, suffix ...string) (string, bool)
- func HasPrefix(s, prefix string) bool
- func HasSuffix(s, suffix string) bool
- func Index(s, sep string) int
- func IndexAny(s, chars string) int
- func IndexByte(s string, c byte) int
- func IndexForward(s, sep string, start int) int
- func IndexFunc(s string, f func(rune) bool) int
- func IndexLast(s, sep string) int
- func IndexLastAny(s, chars string) int
- func IndexLastFunc(s string, f func(rune) bool) int
- func IndexLastOrdinal(s, sep string, ordinal int) int
- func IndexLastStart(s, sep string, start int) int
- func IndexOrdinal(s, sep string, ordinal int) int
- func IndexRune(s string, r rune) int
- func IndexStart(s, sep string, start int) int
- func IsAnyBlank(s ...string) bool
- func IsBlank(s string) bool
- func IsNoneBlank(s ...string) bool
- func IsNotBlank(s string) bool
- func Join(a []string, sep string) string
- func JoinIgnoreBlank(a []string, sep string) string
- func Left(s string, length int) string
- func Map(mapping func(rune) rune, s string) string
- func Mid(s string, start, length int) string
- func NewReader(s string) *strings.Reader
- func NewReplacer(oldnew ...string) *strings.Replacer
- func Overlay(str, overlay string, start, end int) string
- func Pad(in string, size int) string
- func PadEnd(s string, size int) string
- func PadLeft(in string, size int, sep string) string
- func PadRight(in string, size int, sep string) string
- func PadStart(s string, size int) string
- func Remove(s, remove string) string
- func RemoveBlank(s string) (d string)
- func RemoveEnd(s, remove string) string
- func RemoveSpace(s string) (d string)
- func RemoveStart(s, remove string) string
- func Repeat(s string, count int) string
- func Replace(s, old, new string, n int) string
- func Right(s string, length int) string
- func Runes(s string) []rune
- func Slice(s string, start, end int) string
- func Split(s, sep string) []string
- func SplitAfter(s, sep string) []string
- func SplitAfterN(s, sep string, n int) []string
- func SplitN(s, sep string, n int) []string
- func Title(s string) string
- func ToLower(s string) string
- func ToLowerFirst(s string) string
- func ToLowerSpecial(c unicode.SpecialCase, s string) string
- func ToTitle(s string) string
- func ToTitleSpecial(c unicode.SpecialCase, s string) string
- func ToUpper(s string) string
- func ToUpperFirst(s string) string
- func ToUpperSpecial(c unicode.SpecialCase, s string) string
- func Trim(s string, cutset string) string
- func TrimFunc(s string, f func(rune) bool) string
- func TrimLeft(s string, cutset string) string
- func TrimLeftFunc(s string, f func(rune) bool) string
- func TrimPrefix(s, prefix string) string
- func TrimRight(s string, cutset string) string
- func TrimRightFunc(s string, f func(rune) bool) string
- func TrimSpace(s string) string
- func TrimSpaceNQuote1(s string) string
- func TrimSuffix(s, suffix string) string
- func UnCamel(s, sep string) string
Examples ¶
- Abbr
- After
- AfterLast
- Anon
- Anonymity
- Anonymous
- Before
- BeforeLast
- Between
- BetweenSame
- Betweens
- Camel
- Center
- Contains
- ContainsAny
- ContainsNone
- ContainsOnly
- ContainsRune
- ContainsSlice
- ContainsSliceAny
- ContainsSpace
- EqualFold
- Index
- IndexForward
- IndexLast
- IndexLastOrdinal
- IndexLastStart
- IndexOrdinal
- IndexStart
- IsAnyBlank
- IsBlank
- IsNoneBlank
- IsNotBlank
- Left
- Mid
- Overlay
- Pad
- PadEnd
- PadLeft
- PadRight
- PadStart
- Remove
- RemoveBlank
- RemoveEnd
- RemoveSpace
- RemoveStart
- Repeat
- Right
- Slice
- Split
- SplitAfter
- SplitAfterN
- SplitN
- Title
- ToLower
- ToTitle
- ToUpper
- Trim
- TrimLeft
- TrimRight
- TrimSpace
- TrimSpaceNQuote1
- UnCamel
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Abbr ¶
Abbr abbreviate a String using ellipses. This will turn "goyy" into "g...".
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []struct { in string width int }{ {"", 6}, {"a", 6}, {"abc", 6}, {"AbC123", 6}, {"azAZ09_", 6}, {"世界hello", 6}, } for _, v := range s { fmt.Println(strings.Abbr(v.in, v.width)) } }
Output: a abc AbC123 azA... 世界h...
func After ¶
After gets the substring after the first occurrence of a separator. The separator is not returned.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.After("abc", "a")) fmt.Println(strings.After("abcba", "b")) fmt.Println(strings.After("abcba", "e")) }
Output: bc cba
func AfterLast ¶
AfterLast gets the substring after the last occurrence of a separator. The separator is not returned.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.AfterLast("abc", "a")) fmt.Println(strings.AfterLast("abcba", "b")) }
Output: bc a
func Anon ¶
Anon anonymous a String using asterisk. This will turn "goyy" into "g***y".
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []string{"", "a", "ac", "abc", "AbC123", "azAZ09_", "世界hello"} for _, v := range s { fmt.Println(strings.Anon(v)) } }
Output: a a***c a***c A***3 a***_ 世***o
func Anonymity ¶
Anonymity a String using asterisk. This will turn "15566668888" into "155****8888".
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []string{"", "abc", "15566668888", "abcdefghijk", "abcdefghijklmn"} for _, v := range s { fmt.Println(strings.Anonymity(v)) } }
Output: abc 155****8888 abc****hijk abc****klmn
func Anonymous ¶
Anonymous a String using asterisk. This will turn "goyy" into "g**y".
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []struct { in string left, right, star int }{ {"", 1, 1, 0}, {"a", -1, -1, 10}, {"abc", -1, -1, 3}, {"abcd", 5, 5, 3}, {"AbC123", 1, 1, 0}, {"azAZ09_", 1, 1, 3}, {"世界hello", 1, 2, 3}, } for _, v := range s { fmt.Println(strings.Anonymous(v.in, v.left, v.right, v.star)) } }
Output: a a***c abcd A****3 a***_ 世***lo
func Before ¶
Before gets the substring before the first occurrence of a separator. The separator is not returned.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Before("abc", "c")) fmt.Println(strings.Before("abcba", "b")) }
Output: ab a
func BeforeLast ¶
BeforeLast gets the substring before the last occurrence of a separator. The separator is not returned.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.BeforeLast("abc", "c")) fmt.Println(strings.BeforeLast("abcba", "b")) }
Output: ab abc
func Between ¶
Between gets the string that is nested in between two string. Only the first match is returned.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Between("yabcz", "y", "z")) fmt.Println(strings.Between("yabczydefz", "y", "z")) }
Output: abc abc
func BetweenSame ¶
BetweenSame gets the string that is nested in between two instances of the same String.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.BetweenSame("tagabctag", "tag")) }
Output: abc
func Betweens ¶
Betweens gets the slice string that is nested in between two string. Only the first match is returned. eg. strings.Betweens("[a][b][c]", "[", "]") = [a b c] strings.Betweens("1(aa)2(bb)3(cc)4", "{", "}") = [aa bb cc] strings.Betweens(" 1${id}2 3${name} ${pwd}4", "${", "}") = [id name pwd]
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Betweens("[a][b][c]", "[", "]")) fmt.Println(strings.Betweens("1(aa)2(bb)3(cc)4", "(", ")")) }
Output: [a b c] [aa bb cc]
func Camel ¶
Camel returns a copy of the string s with all Unicode letters that begin words mapped to their camel case.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []string{"", "a", "cat", "cAt", " aaa aaa aaa ", "_Aaa_Aaa_Aaa_", "123a456", "douBle-blind", "ÿøû"} for _, v := range s { fmt.Println(strings.Camel(v)) } }
Output: A Cat Cat AaaAaaAaa AaaAaaAaa 123a456 DoubleBlind Ÿøû
func Center ¶
Center centers a string in a larger string of size.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Center("bat", 8, "tag")) }
Output: tabattag
func Contains ¶
Contains returns true if substr is within s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Contains("seafood", "foo")) fmt.Println(strings.Contains("seafood", "bar")) fmt.Println(strings.Contains("seafood", "")) fmt.Println(strings.Contains("", "")) fmt.Println(strings.Contains("", "foo")) }
Output: true false true true false
func ContainsAny ¶
ContainsAny returns true if any Unicode code points in chars are within s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.ContainsAny("team", "i")) fmt.Println(strings.ContainsAny("failure", "u & i")) fmt.Println(strings.ContainsAny("foo", "")) fmt.Println(strings.ContainsAny("", "")) }
Output: false true false false
func ContainsNone ¶
ContainsNone checks that the string does not contain certain chars.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.ContainsNone("abab", "xyz")) fmt.Println(strings.ContainsNone("ab1", "xyz")) fmt.Println(strings.ContainsNone("abz", "xyz")) }
Output: true true false
func ContainsOnly ¶
ContainsOnly checks if the string contains only certain chars.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.ContainsOnly("abab", "abc")) fmt.Println(strings.ContainsOnly("ab1", "abc")) fmt.Println(strings.ContainsOnly("abz", "abc")) }
Output: true false false
func ContainsRune ¶
ContainsRune returns true if the Unicode code point r is within s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.ContainsRune("team", 'i')) fmt.Println(strings.ContainsRune("failure", 'u')) }
Output: false true
func ContainsSlice ¶
ContainsSlice returns true if chars is within s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.ContainsSlice("abcd", []string{"ab", "cd"})) fmt.Println(strings.ContainsSlice("ab1", []string{"ab", "12"})) fmt.Println(strings.ContainsSlice("abz", []string{"xy", "cd"})) fmt.Println(strings.ContainsSlice("abz", []string{"", ""})) fmt.Println(strings.ContainsSlice("", []string{"xy", "cd"})) }
Output: true false false true false
func ContainsSliceAny ¶
ContainsSliceAny returns true if any Unicode code points in chars are within s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.ContainsSliceAny("abab", []string{"ab", "cd"})) fmt.Println(strings.ContainsSliceAny("ab1", []string{"abc", "12"})) fmt.Println(strings.ContainsSliceAny("abz", []string{"xy", "cd"})) fmt.Println(strings.ContainsSliceAny("abz", []string{"", ""})) fmt.Println(strings.ContainsSliceAny("", []string{"xy", "cd"})) }
Output: true false false true false
func ContainsSpace ¶
ContainsSpace contains returns true if whitespace is within s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.ContainsSpace("a")) fmt.Println(strings.ContainsSpace(" a ")) fmt.Println(strings.ContainsSpace("ab c")) fmt.Println(strings.ContainsSpace("ab\tc")) fmt.Println(strings.ContainsSpace("ab\rc")) fmt.Println(strings.ContainsSpace("ab\nc")) }
Output: false true true true true true
func EqualFold ¶
EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.EqualFold("Go", "go")) }
Output: true
func Fields ¶
Fields splits the string s around each instance of one or more consecutive white space characters, returning an array of substrings of s or an empty list if s contains only white space.
func FieldsFunc ¶
FieldsFunc splits the string s at each run of Unicode code points c satisfying f(c) and returns an array of slices of s. If all code points in s satisfy f(c) or the string is empty, an empty slice is returned.
func FieldsSpace ¶
FieldsSpace splits the string s around each instance of one or more consecutive white space characters, returning an array of substrings of s or an empty list if s contains only white space.
func HasAnyPrefix ¶
HasAnyPrefix tests whether the string s begins with prefix.
func HasAnySuffix ¶
HasAnySuffix tests whether the string s ends with suffix.
func Index ¶
Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Index("abABabABabAB", "ab")) fmt.Println(strings.Index("abcdABCDabcd", "cd")) fmt.Println(strings.Index("abcdABCDabcd", "de")) }
Output: 0 2 -1
func IndexAny ¶
IndexAny returns the index of the first instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
func IndexByte ¶
IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
func IndexForward ¶
IndexForward returns the forward index of the first instance of sep in s from a start position, or -1 if sep is not present in s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IndexForward("abABabABabAB", "ab", 6)) fmt.Println(strings.IndexForward("abcdABCDabcd", "cd", 5)) fmt.Println(strings.IndexForward("abcdABCDabcd", "de", 0)) }
Output: 4 2 -1
func IndexFunc ¶
IndexFunc returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.
func IndexLast ¶
IndexLast returns the index of the last instance of sep in s, or -1 if sep is not present in s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IndexLast("abABabABabAB", "ab")) fmt.Println(strings.IndexLast("abcdABCDabcd", "cd")) fmt.Println(strings.IndexLast("abcdABCDabcd", "de")) }
Output: 8 10 -1
func IndexLastAny ¶
IndexLastAny returns the index of the last instance of any Unicode code point from chars in s, or -1 if no Unicode code point from chars is present in s.
func IndexLastFunc ¶
IndexLastFunc returns the index into s of the last Unicode code point satisfying f(c), or -1 if none do.
func IndexLastOrdinal ¶
IndexLastOrdinal returns the index of the n-th last instance of sep in s, or -1 if sep is not present in s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IndexLastOrdinal("abABabABabAB", "ab", 2)) fmt.Println(strings.IndexLastOrdinal("abcdABCDabcd", "cd", 2)) fmt.Println(strings.IndexLastOrdinal("abcdABCDabcd", "de", 1)) }
Output: 4 2 -1
func IndexLastStart ¶
IndexLastStart returns the index of the last instance of sep in s from a start position, or -1 if sep is not present in s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IndexLastStart("abABabABabAB", "ab", 6)) fmt.Println(strings.IndexLastStart("abcdABCDabcd", "cd", 5)) fmt.Println(strings.IndexLastStart("abcdABCDabcd", "de", 0)) }
Output: 4 2 -1
func IndexOrdinal ¶
IndexOrdinal returns the index of the n-th instance of sep in s, or -1 if sep is not present in s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IndexOrdinal("abABabABabAB", "ab", 3)) fmt.Println(strings.IndexOrdinal("abcdABCDabcd", "cd", 2)) fmt.Println(strings.IndexOrdinal("abcdABCDabcd", "de", 1)) }
Output: 8 10 -1
func IndexRune ¶
IndexRune returns the index of the first instance of the Unicode code point r, or -1 if rune is not present in s.
func IndexStart ¶
IndexStart returns the index of the first instance of sep in s from a start position, or -1 if sep is not present in s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IndexStart("abABabABabAB", "ab", 6)) fmt.Println(strings.IndexStart("abcdABCDabcd", "cd", 5)) fmt.Println(strings.IndexStart("abcdABCDabcd", "de", 0)) }
Output: 8 10 -1
func IsAnyBlank ¶
IsAnyBlank checks if any one of the string are empty ("") or whitespace.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IsAnyBlank("", " ")) fmt.Println(strings.IsAnyBlank(" ", " a ")) fmt.Println(strings.IsAnyBlank(" \t\r\n ", " a ")) fmt.Println(strings.IsAnyBlank("a", " a ")) fmt.Println(strings.IsAnyBlank(" a ", " a ")) fmt.Println(strings.IsAnyBlank(" \t\r\n a \t\r\n ", " a ")) }
Output: true true true false false false
func IsBlank ¶
IsBlank checks if a string is empty ("") or whitespace.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IsBlank("")) fmt.Println(strings.IsBlank(" ")) fmt.Println(strings.IsBlank(" \t\r\n ")) fmt.Println(strings.IsBlank("a")) fmt.Println(strings.IsBlank(" a ")) fmt.Println(strings.IsBlank(" \t\r\n a \t\r\n ")) }
Output: true true true false false false
func IsNoneBlank ¶
IsNoneBlank checks if none of the string are empty ("") or whitespace
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IsNoneBlank("", " ")) fmt.Println(strings.IsNoneBlank(" ", " a ")) fmt.Println(strings.IsNoneBlank(" \t\r\n ", " a ")) fmt.Println(strings.IsNoneBlank("a", " a ")) fmt.Println(strings.IsNoneBlank(" a ", " a ")) fmt.Println(strings.IsNoneBlank(" \t\r\n a \t\r\n ", " a ")) }
Output: false false false true true true
func IsNotBlank ¶
IsNotBlank checks if a string is not empty ("") and not whitespace only.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.IsNotBlank("")) fmt.Println(strings.IsNotBlank(" ")) fmt.Println(strings.IsNotBlank(" \t\r\n ")) fmt.Println(strings.IsNotBlank("a")) fmt.Println(strings.IsNotBlank(" a ")) fmt.Println(strings.IsNotBlank(" \t\r\n a \t\r\n ")) }
Output: false false false true true true
func Join ¶
Join concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.
func JoinIgnoreBlank ¶
JoinIgnoreBlank join concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.
func Left ¶
Left gets the leftmost len characters of a String.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Left("abc", 2)) fmt.Println(strings.Left("abc", 4)) }
Output: ab abc
func Map ¶
Map returns a copy of the string s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.
func Mid ¶
Mid gets len characters from the middle of a String.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Mid("abc", 0, 2)) fmt.Println(strings.Mid("abc", 0, 4)) fmt.Println(strings.Mid("abc", -2, 2)) }
Output: ab abc bc
func NewReader ¶
NewReader returns a new Reader reading from s. It is similar to bytes.NewBufferString but more efficient and read-only.
func NewReplacer ¶
NewReplacer returns a new Replacer from a list of old, new string pairs. Replacements are performed in order, without overlapping matches.
func Overlay ¶
Overlay overlays part of a String with another String.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Overlay("", "abc", 0, 0)) fmt.Println(strings.Overlay("abcdef", "", 2, 4)) fmt.Println(strings.Overlay("abcdef", "", 4, 2)) fmt.Println(strings.Overlay("abcdef", "zzzz", 2, 4)) fmt.Println(strings.Overlay("abcdef", "zzzz", 4, 2)) fmt.Println(strings.Overlay("abcdef", "zzzz", -1, 4)) fmt.Println(strings.Overlay("abcdef", "zzzz", 2, 8)) fmt.Println(strings.Overlay("abcdef", "zzzz", -2, -3)) fmt.Println(strings.Overlay("abcdef", "zzzz", 8, 10)) fmt.Println(strings.Overlay("世界hello", "zzzz", 2, 4)) }
Output: abc abef abef abzzzzef abzzzzef zzzzef abzzzz zzzzabcdef abcdefzzzz 世界zzzzllo
func Pad ¶
Pad pads a string in a larger string of size using the space character (' ').
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("%#q\n", strings.Pad("bat", 5)) }
Output: ` bat `
func PadEnd ¶
PadEnd right pad a String with spaces.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("%#q\n", strings.PadEnd("bat", 5)) }
Output: `bat `
func PadLeft ¶
PadLeft left pad a String with a specified character.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.PadLeft("bat", 5, "*")) }
Output: **bat
func PadRight ¶
PadRight right pad a String with a specified character.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.PadRight("bat", 5, "*")) }
Output: bat**
func PadStart ¶
PadStart left pad a String with spaces.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("%#q\n", strings.PadStart("bat", 5)) }
Output: ` bat`
func Remove ¶
Remove removes all occurrences of a substring from within the source string.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Remove("abc", "")) fmt.Println(strings.Remove("queued", "ue")) fmt.Println(strings.Remove("queued", "zz")) }
Output: abc qd queued
func RemoveBlank ¶
RemoveBlank removes all whitespaces from a string.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.RemoveBlank(" a\nb c\td e \rf g ")) }
Output: ab cd e f g
func RemoveEnd ¶
RemoveEnd removes a substring only if it is at the end of a source string, otherwise returns the source string.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.RemoveEnd("abc", "")) fmt.Println(strings.RemoveEnd("www.domain.com", ".com")) fmt.Println(strings.RemoveEnd("www.domain.com", "domain")) }
Output: abc www.domain www.domain.com
func RemoveSpace ¶
RemoveSpace removes all whitespaces from a string.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.RemoveSpace(" a\nb c\td e \rf g ")) }
Output: abcdefg
func RemoveStart ¶
RemoveStart removes a substring only if it is at the beginning of a source string, otherwise returns the source string.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.RemoveStart("abc", "")) fmt.Println(strings.RemoveStart("www.domain.com", "www.")) fmt.Println(strings.RemoveStart("www.domain.com", "domain")) }
Output: abc domain.com www.domain.com
func Repeat ¶
Repeat returns a new string consisting of count copies of the string s.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Repeat("-", 10)) }
Output: ----------
func Replace ¶
Replace returns a copy of the string s with the first n non-overlapping instances of old replaced by new. If n < 0, there is no limit on the number of replacements.
func Right ¶
Right gets the rightmost len characters of a String.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Right("abc", 2)) fmt.Println(strings.Right("abc", 4)) }
Output: bc abc
func Slice ¶
Slice the returned substring starts with the character in the start position and ends before the end position.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.Slice("abc", 0, 2)) fmt.Println(strings.Slice("abc", 2, 4)) fmt.Println(strings.Slice("abc", -2, -1)) fmt.Println(strings.Slice("abc", -7, -6)) }
Output: ab c b
func Split ¶
Split slices s into all substrings separated by sep and returns a slice of the substrings between those separators. If sep is empty, Split splits after each UTF-8 sequence. It is equivalent to SplitN with a count of -1.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("%q\n", strings.Split("a,b,c", ",")) fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) fmt.Printf("%q\n", strings.Split(" xyz ", "")) fmt.Printf("%q\n", strings.Split(" x y z ", " ")) fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) }
Output: ["a" "b" "c"] ["" "man " "plan " "canal panama"] [" " "x" "y" "z" " "] ["" "x" "" "y" "" "" "z" ""] [""]
func SplitAfter ¶
SplitAfter slices s into all substrings after each instance of sep and returns a slice of those substrings. If sep is empty, SplitAfter splits after each UTF-8 sequence. It is equivalent to SplitAfterN with a count of -1.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) }
Output: ["a," "b," "c"]
func SplitAfterN ¶
SplitAfterN slices s into substrings after each instance of sep and returns a slice of those substrings. If sep is empty, SplitAfterN splits after each UTF-8 sequence. The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder. n == 0: the result is nil (zero substrings) n < 0: all substrings
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) }
Output: ["a," "b,c"]
func SplitN ¶
SplitN slices s into substrings separated by sep and returns a slice of the substrings between those separators. If sep is empty, SplitN splits after each UTF-8 sequence. The count determines the number of substrings to return:
n > 0: at most n substrings; the last substring will be the unsplit remainder. n == 0: the result is nil (zero substrings) n < 0: all substrings
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) z := strings.SplitN("a,b,c", ",", 0) fmt.Printf("%q (nil = %v)\n", z, z == nil) }
Output: ["a" "b,c"] [] (nil = true)
func Title ¶
Title returns a copy of the string s with all Unicode letters that begin words mapped to their title case.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []string{"", "a", "cat", "cAt", "aaa aaa aaa", "Aaa Aaa Aaa", "123a456", "double-blind", "ÿøû"} for _, v := range s { fmt.Println(strings.Title(v)) } }
Output: A Cat CAt Aaa Aaa Aaa Aaa Aaa Aaa 123a456 Double-Blind Ÿøû
func ToLower ¶
ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []string{"", "abc", "AbC123", "azAZ09_"} for _, v := range s { fmt.Println(strings.ToLower(v)) } }
Output: abc abc123 azaz09_
func ToLowerFirst ¶
ToLowerFirst returns a copy of the string s with first Unicode letters mapped to their lower case.
func ToLowerSpecial ¶
func ToLowerSpecial(c unicode.SpecialCase, s string) string
ToLowerSpecial returns a copy of the string s with all Unicode letters mapped to their lower case, giving priority to the special casing rules.
func ToTitle ¶
ToTitle returns a copy of the string s with all Unicode letters mapped to their title case.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []string{"", "a", "cat", "cAt", "aaa aaa aaa", "Aaa Aaa Aaa", "123a456", "double-blind", "ÿøû"} for _, v := range s { fmt.Println(strings.ToTitle(v)) } }
Output: A CAT CAT AAA AAA AAA AAA AAA AAA 123A456 DOUBLE-BLIND ŸØÛ
func ToTitleSpecial ¶
func ToTitleSpecial(c unicode.SpecialCase, s string) string
ToTitleSpecial returns a copy of the string s with all Unicode letters mapped to their title case, giving priority to the special casing rules.
func ToUpper ¶
ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []string{"", "abc", "AbC123", "azAZ09_"} for _, v := range s { fmt.Println(strings.ToUpper(v)) } }
Output: ABC ABC123 AZAZ09_
func ToUpperFirst ¶
ToUpperFirst returns a copy of the string s with first Unicode letters mapped to their upper case.
func ToUpperSpecial ¶
func ToUpperSpecial(c unicode.SpecialCase, s string) string
ToUpperSpecial returns a copy of the string s with all Unicode letters mapped to their upper case, giving priority to the special casing rules.
func Trim ¶
Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("[%q]\n", strings.Trim("abba", "")) fmt.Printf("[%q]\n", strings.Trim(" !!! Achtung !!! ", "! ")) }
Output: ["abba"] ["Achtung"]
func TrimFunc ¶
TrimFunc returns a slice of the string s with all leading and trailing Unicode code points c satisfying f(c) removed.
func TrimLeft ¶
TrimLeft returns a slice of the string s with all leading Unicode code points contained in cutset removed.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("[%q]\n", strings.TrimLeft("abba", "")) fmt.Printf("[%q]\n", strings.TrimLeft(" !!! Achtung !!! ", "! ")) }
Output: ["abba"] ["Achtung !!! "]
func TrimLeftFunc ¶
TrimLeftFunc returns a slice of the string s with all leading Unicode code points c satisfying f(c) removed.
func TrimPrefix ¶
TrimPrefix returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.
func TrimRight ¶
TrimRight returns a slice of the string s, with all trailing Unicode code points contained in cutset removed.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Printf("[%q]\n", strings.TrimRight("abba", "")) fmt.Printf("[%q]\n", strings.TrimRight(" !!! Achtung !!! ", "! ")) }
Output: ["abba"] [" !!! Achtung"]
func TrimRightFunc ¶
TrimRightFunc returns a slice of the string s with all trailing Unicode code points c satisfying f(c) removed.
func TrimSpace ¶
TrimSpace returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.TrimSpace(" \t\n a lone gopher \n\t\r\n")) }
Output: a lone gopher
func TrimSpaceNQuote1 ¶
TrimSpaceNQuote1 returns a slice of the string s, with all leading and trailing white space or quote removed, as defined by Unicode.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { fmt.Println(strings.TrimSpaceNQuote1(" ")) fmt.Println(strings.TrimSpaceNQuote1(" ` ` ")) fmt.Println(strings.TrimSpaceNQuote1(" ' ' ")) fmt.Println(strings.TrimSpaceNQuote1("aaa")) fmt.Println(strings.TrimSpaceNQuote1(" aaa ")) fmt.Println(strings.TrimSpaceNQuote1("`aaa`")) fmt.Println(strings.TrimSpaceNQuote1("'aaa'")) fmt.Println(strings.TrimSpaceNQuote1(" ` aaa ` ")) fmt.Println(strings.TrimSpaceNQuote1(" `' aaa '` ")) fmt.Println(strings.TrimSpaceNQuote1(" ` ' aaa ' ` ")) fmt.Println(strings.TrimSpaceNQuote1(" \t\n ' a lone gopher ' \n\t\r\n")) fmt.Println(strings.TrimSpaceNQuote1(" \t\n '` a lone gopher `' \n\t\r\n")) }
Output: aaa aaa aaa aaa aaa ' aaa ' ' aaa ' a lone gopher ` a lone gopher `
func TrimSuffix ¶
TrimSuffix returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.
func UnCamel ¶
UnCamel returns a copy of the string s with all Unicode letters that begin words mapped to their uncamel case.
Example ¶
package main import ( "fmt" "gopkg.in/goyy/goyy.v0/util/strings" ) func main() { s := []string{"", "A", "Cat", "cAt", "AaaAaaAaa", " AaaAaaAaa ", "123a456", "DoubleBlind", "Ÿøû"} for _, v := range s { fmt.Println(strings.UnCamel(v, "_")) } }
Output: a cat c_at aaa_aaa_aaa aaa_aaa_aaa 123a456 double_blind ÿøû
Types ¶
This section is empty.