Documentation ¶
Overview ¶
Package gstr provides functions for string handling.
Index ¶
- Constants
- func AddSlashes(str string) string
- func CaseCamel(s string) string
- func CaseCamelLower(s string) string
- func CaseConvert(s string, caseType CaseType) string
- func CaseDelimited(s string, del byte) string
- func CaseDelimitedScreaming(s string, del uint8, screaming bool) string
- func CaseKebab(s string) string
- func CaseKebabScreaming(s string) string
- func CaseSnake(s string) string
- func CaseSnakeFirstUpper(word string, underscore ...string) string
- func CaseSnakeScreaming(s string) string
- func Chr(ascii int) string
- func ChunkSplit(body string, chunkLen int, end string) string
- func Compare(a, b string) int
- func CompareVersion(a, b string) int
- func CompareVersionGo(a, b string) int
- func Contains(str, substr string) bool
- func ContainsAny(s, chars string) bool
- func ContainsI(str, substr string) bool
- func Count(s, substr string) int
- func CountChars(str string, noSpace ...bool) map[string]int
- func CountI(s, substr string) int
- func CountWords(str string) map[string]int
- func Equal(a, b string) bool
- func Explode(delimiter, str string) []string
- func Fields(str string) []string
- func HasPrefix(s, prefix string) bool
- func HasSuffix(s, suffix string) bool
- func HideStr(str string, percent int, hide string) string
- func Implode(glue string, pieces []string) string
- func InArray(a []string, s string) bool
- func IsGNUVersion(version string) bool
- func IsLetterLower(b byte) bool
- func IsLetterUpper(b byte) bool
- func IsNumeric(s string) bool
- func IsSubDomain(subDomain string, mainDomain string) bool
- func Join(array []string, sep string) string
- func JoinAny(array interface{}, sep string) string
- func LcFirst(s string) string
- func LenRune(str string) int
- func Levenshtein(str1, str2 string, costIns, costRep, costDel int) int
- func List2(str, delimiter string) (part1, part2 string)
- func List3(str, delimiter string) (part1, part2, part3 string)
- func ListAndTrim2(str, delimiter string) (part1, part2 string)
- func ListAndTrim3(str, delimiter string) (part1, part2, part3 string)
- func Nl2Br(str string, isXhtml ...bool) string
- func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string
- func OctStr(str string) string
- func Ord(char string) int
- func Parse(s string) (result map[string]interface{}, err error)
- func Pos(haystack, needle string, startOffset ...int) int
- func PosI(haystack, needle string, startOffset ...int) int
- func PosIRune(haystack, needle string, startOffset ...int) int
- func PosR(haystack, needle string, startOffset ...int) int
- func PosRI(haystack, needle string, startOffset ...int) int
- func PosRIRune(haystack, needle string, startOffset ...int) int
- func PosRRune(haystack, needle string, startOffset ...int) int
- func PosRune(haystack, needle string, startOffset ...int) int
- func PrefixArray(array []string, prefix string)
- func QuoteMeta(str string, chars ...string) string
- func Repeat(input string, multiplier int) string
- func Replace(origin, search, replace string, count ...int) string
- func ReplaceByArray(origin string, array []string) string
- func ReplaceByMap(origin string, replaces map[string]string) string
- func ReplaceI(origin, search, replace string, count ...int) string
- func ReplaceIByArray(origin string, array []string) string
- func ReplaceIByMap(origin string, replaces map[string]string) string
- func Reverse(str string) string
- func SearchArray(a []string, s string) int
- func Shuffle(str string) string
- func SimilarText(first, second string, percent *float64) int
- func Soundex(str string) string
- func Split(str, delimiter string) []string
- func SplitAndTrim(str, delimiter string, characterMask ...string) []string
- func Str(haystack string, needle string) string
- func StrEx(haystack string, needle string) string
- func StrLimit(str string, length int, suffix ...string) string
- func StrLimitRune(str string, length int, suffix ...string) string
- func StrTill(haystack string, needle string) string
- func StrTillEx(haystack string, needle string) string
- func StripSlashes(str string) string
- func SubStr(str string, start int, length ...int) (substr string)
- func SubStrFrom(str string, need string) (substr string)
- func SubStrFromEx(str string, need string) (substr string)
- func SubStrFromR(str string, need string) (substr string)
- func SubStrFromREx(str string, need string) (substr string)
- func SubStrRune(str string, start int, length ...int) (substr string)
- func ToLower(s string) string
- func ToUpper(s string) string
- func Trim(str string, characterMask ...string) string
- func TrimAll(str string, characterMask ...string) string
- func TrimLeft(str string, characterMask ...string) string
- func TrimLeftStr(str string, cut string, count ...int) string
- func TrimRight(str string, characterMask ...string) string
- func TrimRightStr(str string, cut string, count ...int) string
- func TrimStr(str string, cut string, count ...int) string
- func UcFirst(s string) string
- func UcWords(str string) string
- func WordWrap(str string, width int, br string) string
- type CaseType
Examples ¶
- AddSlashes
- CaseCamel
- CaseCamelLower
- CaseDelimited
- CaseDelimitedScreaming
- CaseKebab
- CaseKebabScreaming
- CaseSnake
- CaseSnakeFirstUpper
- CaseSnakeScreaming
- Chr
- ChunkSplit
- Compare
- CompareVersion
- CompareVersionGo
- Contains
- ContainsAny
- ContainsI
- Count
- CountChars
- CountI
- CountWords
- Equal
- Explode
- Fields
- HasPrefix
- HasSuffix
- HideStr
- Implode
- InArray
- IsLetterLower
- IsLetterUpper
- IsNumeric
- IsSubDomain
- Join
- JoinAny
- LcFirst
- LenRune
- Levenshtein
- Nl2Br
- NumberFormat
- OctStr
- Ord
- Parse
- Pos
- PosI
- PosIRune
- PosR
- PosRI
- PosRIRune
- PosRRune
- PosRune
- PrefixArray
- QuoteMeta
- Repeat
- Replace
- ReplaceByArray
- ReplaceByMap
- ReplaceI
- ReplaceIByArray
- ReplaceIByMap
- Reverse
- SearchArray
- Shuffle
- SimilarText
- Soundex
- Split
- SplitAndTrim
- Str
- StrEx
- StrLimit
- StrLimitRune
- StrTill
- StrTillEx
- StripSlashes
- SubStr
- SubStrFrom
- SubStrFromEx
- SubStrFromR
- SubStrFromREx
- SubStrRune
- ToLower
- ToUpper
- Trim
- TrimAll
- TrimLeft
- TrimLeftStr
- TrimRight
- TrimRightStr
- TrimStr
- UcFirst
- UcWords
- WordWrap
Constants ¶
const (
// NotFoundIndex is the position index for string not found in searching functions.
NotFoundIndex = -1
)
Variables ¶
This section is empty.
Functions ¶
func AddSlashes ¶
AddSlashes quotes with slashes `\` for chars: '"\.
func CaseCamel ¶
CaseCamel converts a string to CamelCase.
Example: CaseCamel("any_kind_of_string") -> AnyKindOfString
func CaseCamelLower ¶
CaseCamelLower converts a string to lowerCamelCase.
Example: CaseCamelLower("any_kind_of_string") -> anyKindOfString
func CaseConvert ¶ added in v2.5.7
CaseConvert converts a string to the specified naming convention. Use CaseTypeMatch to match the case type from string.
func CaseDelimited ¶
CaseDelimited converts a string to snake.case.delimited.
Example: CaseDelimited("AnyKindOfString", '.') -> any.kind.of.string
func CaseDelimitedScreaming ¶
CaseDelimitedScreaming converts a string to DELIMITED.SCREAMING.CASE or delimited.screaming.case.
Example: CaseDelimitedScreaming("AnyKindOfString", '.') -> ANY.KIND.OF.STRING
func CaseKebab ¶
CaseKebab converts a string to kebab-case.
Example: CaseKebab("AnyKindOfString") -> any-kind-of-string
func CaseKebabScreaming ¶
CaseKebabScreaming converts a string to KEBAB-CASE-SCREAMING.
Example: CaseKebab("AnyKindOfString") -> ANY-KIND-OF-STRING
func CaseSnake ¶
CaseSnake converts a string to snake_case.
Example: CaseSnake("AnyKindOfString") -> any_kind_of_string
func CaseSnakeFirstUpper ¶
CaseSnakeFirstUpper converts a string like "RGBCodeMd5" to "rgb_code_md5". TODO for efficiency should change regexp to traversing string in future.
Example: CaseSnakeFirstUpper("RGBCodeMd5") -> rgb_code_md5
func CaseSnakeScreaming ¶
CaseSnakeScreaming converts a string to SNAKE_CASE_SCREAMING.
Example: CaseSnakeScreaming("AnyKindOfString") -> ANY_KIND_OF_STRING
func ChunkSplit ¶
ChunkSplit splits a string into smaller chunks. Can be used to split a string into smaller chunks which is useful for e.g. converting BASE64 string output to match RFC 2045 semantics. It inserts end every chunkLen characters. It considers parameter `body` and `end` as unicode string.
func Compare ¶
Compare returns an integer comparing two strings lexicographically. The result will be 0 if a==b, -1 if a < b, and +1 if a > b.
func CompareVersion ¶
CompareVersion compares `a` and `b` as standard GNU version.
It returns 1 if `a` > `b`.
It returns -1 if `a` < `b`.
It returns 0 if `a` = `b`.
GNU standard version is like: v1.0 1 1.0.0 v1.0.1 v2.10.8 10.2.0 etc.
func CompareVersionGo ¶
CompareVersionGo compares `a` and `b` as standard Golang version.
It returns 1 if `a` > `b`.
It returns -1 if `a` < `b`.
It returns 0 if `a` = `b`.
Golang standard version is like: 1.0.0 v1.0.1 v2.10.8 10.2.0 v0.0.0-20190626092158-b2ccc519800e v1.12.2-0.20200413154443-b17e3a6804fa v4.20.0+incompatible etc.
func ContainsAny ¶
ContainsAny reports whether any Unicode code points in `chars` are within `s`.
func Count ¶
Count counts the number of `substr` appears in `s`. It returns 0 if no `substr` found in `s`.
func CountChars ¶
CountChars returns information about chars' count used in a string. It considers parameter `str` as unicode string.
func CountI ¶
CountI counts the number of `substr` appears in `s`, case-insensitively. It returns 0 if no `substr` found in `s`.
func CountWords ¶
CountWords returns information about words' count used in a string. It considers parameter `str` as unicode string.
Example ¶
Output: map[string]int{"easy":1, "goframe":1, "is":1, "to":1, "use!":1, "very":1, "very,":1}
func Equal ¶
Equal reports whether `a` and `b`, interpreted as UTF-8 strings, are equal under Unicode case-folding, case-insensitively.
func Explode ¶
Explode splits string `str` by a string `delimiter`, to an array. See http://php.net/manual/en/function.explode.php.
func HideStr ¶
HideStr replaces part of the string `str` to `hide` by `percentage` from the `middle`. It considers parameter `str` as unicode string.
func Implode ¶
Implode joins array elements `pieces` with a string `glue`. http://php.net/manual/en/function.implode.php
func IsGNUVersion ¶ added in v2.1.2
IsGNUVersion checks and returns whether given `version` is valid GNU version string.
func IsLetterLower ¶
IsLetterLower tests whether the given byte b is in lower case.
func IsLetterUpper ¶
IsLetterUpper tests whether the given byte b is in upper case.
func IsSubDomain ¶
IsSubDomain checks whether `subDomain` is sub-domain of mainDomain. It supports '*' in `mainDomain`.
func Join ¶
Join concatenates the elements of `array` to create a single string. The separator string `sep` is placed between elements in the resulting string.
func JoinAny ¶
JoinAny concatenates the elements of `array` to create a single string. The separator string `sep` is placed between elements in the resulting string.
The parameter `array` can be any type of slice, which be converted to string array.
func LcFirst ¶
LcFirst returns a copy of the string s with the first letter mapped to its lower case.
func Levenshtein ¶
Levenshtein calculates Levenshtein distance between two strings. costIns: Defines the cost of insertion. costRep: Defines the cost of replacement. costDel: Defines the cost of deletion. See http://php.net/manual/en/function.levenshtein.php.
func List2 ¶ added in v2.5.5
List2 Split the `str` with `delimiter` and returns the result as two parts string.
func List3 ¶ added in v2.5.5
List3 Split the `str` with `delimiter` and returns the result as three parts string.
func ListAndTrim2 ¶ added in v2.5.5
ListAndTrim2 SplitAndTrim the `str` with `delimiter` and returns the result as two parts string.
func ListAndTrim3 ¶ added in v2.5.5
ListAndTrim3 SplitAndTrim the `str` with `delimiter` and returns the result as three parts string.
func Nl2Br ¶
Nl2Br inserts HTML line breaks(`br`|<br />) before all newlines in a string: \n\r, \r\n, \r, \n. It considers parameter `str` as unicode string.
func NumberFormat ¶
NumberFormat formats a number with grouped thousands. Parameter `decimals`: Sets the number of decimal points. Parameter `decPoint`: Sets the separator for the decimal point. Parameter `thousandsSep`: Sets the thousands' separator. See http://php.net/manual/en/function.number-format.php.
Example: NumberFormat(1234.56, 2, ".", "") -> 1234,56 NumberFormat(1234.56, 2, ",", " ") -> 1 234,56
func OctStr ¶
OctStr converts string container octal string to its original string, for example, to Chinese string.
Example: OctStr("\346\200\241") -> 怡
func Ord ¶
Ord converts the first byte of a string to a value between 0 and 255.
Example: Chr("A") -> 65
func Parse ¶
Parse parses the string into map[string]interface{}.
v1=m&v2=n -> map[v1:m v2:n] v[a]=m&v[b]=n -> map[v:map[a:m b:n]] v[a][a]=m&v[a][b]=n -> map[v:map[a:map[a:m b:n]]] v[]=m&v[]=n -> map[v:[m n]] v[a][]=m&v[a][]=n -> map[v:map[a:[m n]]] v[][]=m&v[][]=n -> map[v:[map[]]] // Currently does not support nested slice. v=m&v[a]=n -> error a .[[b=c -> map[a___[b:c]
func Pos ¶
Pos returns the position of the first occurrence of `needle` in `haystack` from `startOffset`, case-sensitively. It returns -1, if not found.
func PosI ¶
PosI returns the position of the first occurrence of `needle` in `haystack` from `startOffset`, case-insensitively. It returns -1, if not found.
func PosIRune ¶
PosIRune acts like function PosI but considers `haystack` and `needle` as unicode string.
func PosR ¶
PosR returns the position of the last occurrence of `needle` in `haystack` from `startOffset`, case-sensitively. It returns -1, if not found.
func PosRI ¶
PosRI returns the position of the last occurrence of `needle` in `haystack` from `startOffset`, case-insensitively. It returns -1, if not found.
func PosRIRune ¶
PosRIRune acts like function PosRI but considers `haystack` and `needle` as unicode string.
func PosRRune ¶
PosRRune acts like function PosR but considers `haystack` and `needle` as unicode string.
func PosRune ¶
PosRune acts like function Pos but considers `haystack` and `needle` as unicode string.
func PrefixArray ¶
PrefixArray adds `prefix` string for each item of `array`.
Example: PrefixArray(["a","b"], "gf_") -> ["gf_a", "gf_b"]
func QuoteMeta ¶
QuoteMeta returns a version of `str` with a backslash character (`\`). If custom chars `chars` not given, it uses default chars: .\+*?[^]($)
func Repeat ¶
Repeat returns a new string consisting of multiplier copies of the string input.
Example: Repeat("a", 3) -> "aaa"
func Replace ¶
Replace returns a copy of the string `origin` in which string `search` replaced by `replace` case-sensitively.
func ReplaceByArray ¶
ReplaceByArray returns a copy of `origin`, which is replaced by a slice in order, case-sensitively.
func ReplaceByMap ¶
ReplaceByMap returns a copy of `origin`, which is replaced by a map in unordered way, case-sensitively.
func ReplaceI ¶
ReplaceI returns a copy of the string `origin` in which string `search` replaced by `replace` case-insensitively.
func ReplaceIByArray ¶
ReplaceIByArray returns a copy of `origin`, which is replaced by a slice in order, case-insensitively.
func ReplaceIByMap ¶
ReplaceIByMap returns a copy of `origin`, which is replaced by a map in unordered way, case-insensitively.
func Reverse ¶
Reverse returns a string which is the reverse of `str`.
Example: Reverse("123456") -> "654321"
func SearchArray ¶
SearchArray searches string `s` in string slice `a` case-sensitively, returns its index in `a`. If `s` is not found in `a`, it returns -1.
func Shuffle ¶
Shuffle randomly shuffles a string. It considers parameter `str` as unicode string.
Example: Shuffle("123456") -> "325164" Shuffle("123456") -> "231546" ...
func SimilarText ¶
SimilarText calculates the similarity between two strings. See http://php.net/manual/en/function.similar-text.php.
func Soundex ¶
Soundex calculates the soundex key of a string. See http://php.net/manual/en/function.soundex.php.
func SplitAndTrim ¶
SplitAndTrim splits string `str` by a string `delimiter` to an array, and calls Trim to every element of this array. It ignores the elements which are empty after Trim.
func Str ¶
Str returns part of `haystack` string starting from and including the first occurrence of `needle` to the end of `haystack`.
This function performs exactly as function SubStr, but to implement the same function as PHP: http://php.net/manual/en/function.strstr.php.
Example: Str("av.mp4", ".") -> ".mp4"
func StrEx ¶
StrEx returns part of `haystack` string starting from and excluding the first occurrence of `needle` to the end of `haystack`.
This function performs exactly as function SubStrEx, but to implement the same function as PHP: http://php.net/manual/en/function.strstr.php.
Example: StrEx("av.mp4", ".") -> "mp4"
func StrLimit ¶
StrLimit returns a portion of string `str` specified by `length` parameters, if the length of `str` is greater than `length`, then the `suffix` will be appended to the result string.
Example: StrLimit("123456", 3) -> "123..." StrLimit("123456", 3, "~") -> "123~"
func StrLimitRune ¶
StrLimitRune returns a portion of string `str` specified by `length` parameters, if the length of `str` is greater than `length`, then the `suffix` will be appended to the result string. StrLimitRune considers parameter `str` as unicode string.
Example: StrLimitRune("一起学习吧!", 2) -> "一起..." StrLimitRune("一起学习吧!", 2, "~") -> "一起~"
func StrTill ¶
StrTill returns part of `haystack` string ending to and including the first occurrence of `needle` from the start of `haystack`.
Example: StrTill("av.mp4", ".") -> "av."
func StrTillEx ¶
StrTillEx returns part of `haystack` string ending to and excluding the first occurrence of `needle` from the start of `haystack`.
Example: StrTillEx("av.mp4", ".") -> "av"
func StripSlashes ¶
StripSlashes un-quotes a quoted string by AddSlashes.
func SubStr ¶
SubStr returns a portion of string `str` specified by the `start` and `length` parameters. The parameter `length` is optional, it uses the length of `str` in default.
Example: SubStr("123456", 1, 2) -> "23"
func SubStrFrom ¶
SubStrFrom returns a portion of string `str` starting from first occurrence of and including `need` to the end of `str`.
Example: SubStrFrom("av.mp4", ".") -> ".mp4"
func SubStrFromEx ¶
SubStrFromEx returns a portion of string `str` starting from first occurrence of and excluding `need` to the end of `str`.
Example: SubStrFromEx("av.mp4", ".") -> "mp4"
func SubStrFromR ¶
SubStrFromR returns a portion of string `str` starting from last occurrence of and including `need` to the end of `str`.
Example: SubStrFromR("/dev/vda", "/") -> "/vda"
func SubStrFromREx ¶
SubStrFromREx returns a portion of string `str` starting from last occurrence of and excluding `need` to the end of `str`.
Example: SubStrFromREx("/dev/vda", "/") -> "vda"
func SubStrRune ¶
SubStrRune returns a portion of string `str` specified by the `start` and `length` parameters. SubStrRune considers parameter `str` as unicode string. The parameter `length` is optional, it uses the length of `str` in default.
Example: SubStrRune("一起学习吧!", 2, 2) -> "学习"
func ToLower ¶
ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.
func ToUpper ¶
ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.
func Trim ¶
Trim strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.
func TrimLeftStr ¶
TrimLeftStr strips all the given `cut` string from the beginning of a string. Note that it does not strip the whitespaces of its beginning.
func TrimRightStr ¶
TrimRightStr strips all the given `cut` string from the end of a string. Note that it does not strip the whitespaces of its end.
func TrimStr ¶
TrimStr strips all the given `cut` string from the beginning and end of a string. Note that it does not strip the whitespaces of its beginning or end.
func UcFirst ¶
UcFirst returns a copy of the string s with the first letter mapped to its upper case.
func WordWrap ¶
WordWrap wraps a string to a given number of characters. This function supports cut parameters of both english and chinese punctuations. TODO: Enable custom cut parameter, see http://php.net/manual/en/function.wordwrap.php.
Example ¶
Output: A very long woooooooooooooooooord. and something The quick brown fox<br /> jumped over the lazy<br /> dog.
Types ¶
type CaseType ¶ added in v2.5.7
type CaseType string
CaseType is the type for Case.
const ( Camel CaseType = "Camel" CamelLower CaseType = "CamelLower" Snake CaseType = "Snake" SnakeFirstUpper CaseType = "SnakeFirstUpper" SnakeScreaming CaseType = "SnakeScreaming" Kebab CaseType = "Kebab" KebabScreaming CaseType = "KebabScreaming" Lower CaseType = "Lower" )
The case type constants.
func CaseTypeMatch ¶ added in v2.5.7
CaseTypeMatch matches the case type from string.
Source Files ¶
- gstr.go
- gstr_array.go
- gstr_case.go
- gstr_compare.go
- gstr_contain.go
- gstr_convert.go
- gstr_count.go
- gstr_create.go
- gstr_domain.go
- gstr_is.go
- gstr_length.go
- gstr_list.go
- gstr_parse.go
- gstr_pos.go
- gstr_replace.go
- gstr_similar.go
- gstr_slashes.go
- gstr_split_join.go
- gstr_sub.go
- gstr_trim.go
- gstr_upper_lower.go
- gstr_version.go