gstr

package
v1.16.9 Latest Latest
Warning

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

Go to latest
Published: May 30, 2022 License: MIT Imports: 12 Imported by: 183

Documentation

Overview

Package gstr provides functions for string handling.

Index

Constants

View Source
const (
	// NotFoundIndex is the position index for string not found in searching functions.
	NotFoundIndex = -1
)

Variables

This section is empty.

Functions

func AddSlashes

func AddSlashes(str string) string

AddSlashes quotes chars('"\) with slashes.

func CamelCase

func CamelCase(s string) string

CamelCase converts a string to CamelCase. Deprecated, use CaseCamel instead.

func CamelLowerCase

func CamelLowerCase(s string) string

CamelLowerCase converts a string to lowerCamelCase. Deprecated, use CaseCamelLower instead.

func CaseCamel added in v1.15.2

func CaseCamel(s string) string

CaseCamel converts a string to CamelCase.

func CaseCamelLower added in v1.15.2

func CaseCamelLower(s string) string

CaseCamelLower converts a string to lowerCamelCase.

func CaseDelimited added in v1.15.2

func CaseDelimited(s string, del uint8) string

CaseDelimited converts a string to snake.case.delimited.

func CaseDelimitedScreaming added in v1.15.2

func CaseDelimitedScreaming(s string, del uint8, screaming bool) string

CaseDelimitedScreaming converts a string to DELIMITED.SCREAMING.CASE or delimited.screaming.case.

func CaseKebab added in v1.15.2

func CaseKebab(s string) string

CaseKebab converts a string to kebab-case

func CaseKebabScreaming added in v1.15.2

func CaseKebabScreaming(s string) string

CaseKebabScreaming converts a string to KEBAB-CASE-SCREAMING.

func CaseSnake added in v1.15.2

func CaseSnake(s string) string

CaseSnake converts a string to snake_case.

func CaseSnakeFirstUpper added in v1.15.2

func CaseSnakeFirstUpper(word string, underscore ...string) string

CaseSnakeFirstUpper converts a string like "RGBCodeMd5" to "rgb_code_md5". TODO for efficiency should change regexp to traversing string in future.

func CaseSnakeScreaming added in v1.15.2

func CaseSnakeScreaming(s string) string

CaseSnakeScreaming converts a string to SNAKE_CASE_SCREAMING.

func Chr

func Chr(ascii int) string

Chr return the ascii string of a number(0-255).

func ChunkSplit

func ChunkSplit(body string, chunkLen int, end string) string

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

func Compare(a, b string) int

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

func CompareVersion(a, b string) int

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

func CompareVersionGo(a, b string) int

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 v4.20.0+incompatible etc.

func Contains

func Contains(str, substr string) bool

Contains reports whether <substr> is within <str>, case-sensitively.

func ContainsAny

func ContainsAny(s, chars string) bool

ContainsAny reports whether any Unicode code points in <chars> are within <s>.

func ContainsI

func ContainsI(str, substr string) bool

ContainsI reports whether substr is within str, case-insensitively.

func Count

func Count(s, substr string) int

Count counts the number of `substr` appears in `s`. It returns 0 if no `substr` found in `s`.

func CountChars

func CountChars(str string, noSpace ...bool) map[string]int

CountChars returns information about chars' count used in a string. It considers parameter `str` as unicode string.

func CountI

func CountI(s, substr string) int

CountI counts the number of `substr` appears in `s`, case-insensitively. It returns 0 if no `substr` found in `s`.

func CountWords

func CountWords(str string) map[string]int

CountWords returns information about words' count used in a string. It considers parameter `str` as unicode string.

func DelimitedCase

func DelimitedCase(s string, del uint8) string

DelimitedCase converts a string to snake.case.delimited. Deprecated, use CaseDelimited instead.

func DelimitedScreamingCase

func DelimitedScreamingCase(s string, del uint8, screaming bool) string

DelimitedScreamingCase converts a string to DELIMITED.SCREAMING.CASE or delimited.screaming.case. Deprecated, use CaseDelimitedScreaming instead.

func Equal

func Equal(a, b string) bool

Equal reports whether `a` and `b`, interpreted as UTF-8 strings, are equal under Unicode case-folding, case-insensitively.

func Explode

func Explode(delimiter, str string) []string

Explode splits string `str` by a string `delimiter`, to an array. See http://php.net/manual/en/function.explode.php.

func Fields

func Fields(str string) []string

Fields returns the words used in a string as slice.

func HasPrefix added in v1.11.5

func HasPrefix(s, prefix string) bool

HasPrefix tests whether the string s begins with prefix.

func HasSuffix added in v1.11.5

func HasSuffix(s, suffix string) bool

HasSuffix tests whether the string s ends with suffix.

func HideStr

func HideStr(str string, percent int, hide string) string

HideStr replaces part of the string `str` to `hide` by `percentage` from the `middle`. It considers parameter `str` as unicode string.

func Implode

func Implode(glue string, pieces []string) string

Implode joins array elements `pieces` with a string `glue`. http://php.net/manual/en/function.implode.php

func InArray

func InArray(a []string, s string) bool

InArray checks whether string `s` in slice `a`.

func IsLetterLower

func IsLetterLower(b byte) bool

IsLetterLower tests whether the given byte b is in lower case.

func IsLetterUpper

func IsLetterUpper(b byte) bool

IsLetterUpper tests whether the given byte b is in upper case.

func IsNumeric

func IsNumeric(s string) bool

IsNumeric tests whether the given string s is numeric.

func IsSubDomain added in v1.9.3

func IsSubDomain(subDomain string, mainDomain string) bool

IsSubDomain checks whether <subDomain> is sub-domain of mainDomain. It supports '*' in <mainDomain>.

func Join

func Join(array []string, sep string) string

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

func JoinAny(array interface{}, sep string) string

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 KebabCase

func KebabCase(s string) string

KebabCase converts a string to kebab-case. Deprecated, use CaseKebab instead.

func KebabScreamingCase

func KebabScreamingCase(s string) string

KebabScreamingCase converts a string to KEBAB-CASE-SCREAMING. Deprecated, use CaseKebabScreaming instead.

func LcFirst

func LcFirst(s string) string

LcFirst returns a copy of the string s with the first letter mapped to its lower case.

func LenRune added in v1.12.3

func LenRune(str string) int

LenRune returns string length of unicode.

func Levenshtein

func Levenshtein(str1, str2 string, costIns, costRep, costDel int) int

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 Nl2Br

func Nl2Br(str string, isXhtml ...bool) string

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

func NumberFormat(number float64, decimals int, decPoint, thousandsSep string) string

NumberFormat formats a number with grouped thousands. `decimals`: Sets the number of decimal points. `decPoint`: Sets the separator for the decimal point. `thousandsSep`: Sets the thousands' separator. See http://php.net/manual/en/function.number-format.php.

func OctStr added in v1.11.5

func OctStr(str string) string

OctStr converts string container octal string to its original string, for example, to Chinese string. Eg: `\346\200\241` -> 怡

func Ord

func Ord(char string) int

Ord converts the first byte of a string to a value between 0 and 255.

func Parse

func Parse(s string) (result map[string]interface{}, err error)

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

func Pos(haystack, needle string, startOffset ...int) int

Pos returns the position of the first occurrence of `needle` in `haystack` from <startOffset>, case-sensitively. It returns -1, if not found.

func PosI

func PosI(haystack, needle string, startOffset ...int) int

PosI returns the position of the first occurrence of <needle> in <haystack> from <startOffset>, case-insensitively. It returns -1, if not found.

func PosIRune added in v1.12.2

func PosIRune(haystack, needle string, startOffset ...int) int

PosIRune acts like function PosI but considers <haystack> and <needle> as unicode string.

func PosR

func PosR(haystack, needle string, startOffset ...int) int

PosR returns the position of the last occurrence of <needle> in <haystack> from <startOffset>, case-sensitively. It returns -1, if not found.

func PosRI

func PosRI(haystack, needle string, startOffset ...int) int

PosRI returns the position of the last occurrence of <needle> in <haystack> from <startOffset>, case-insensitively. It returns -1, if not found.

func PosRIRune added in v1.12.2

func PosRIRune(haystack, needle string, startOffset ...int) int

PosRIRune acts like function PosRI but considers <haystack> and <needle> as unicode string.

func PosRRune added in v1.12.2

func PosRRune(haystack, needle string, startOffset ...int) int

PosRRune acts like function PosR but considers <haystack> and <needle> as unicode string.

func PosRune added in v1.12.2

func PosRune(haystack, needle string, startOffset ...int) int

PosRune acts like function Pos but considers <haystack> and <needle> as unicode string.

func QuoteMeta

func QuoteMeta(str string, chars ...string) string

QuoteMeta returns a version of str with a backslash character (\) before every character that is among: .\+*?[^]($)

func Repeat

func Repeat(input string, multiplier int) string

Repeat returns a new string consisting of multiplier copies of the string input.

func Replace

func Replace(origin, search, replace string, count ...int) string

Replace returns a copy of the string `origin` in which string `search` replaced by `replace` case-sensitively.

func ReplaceByArray

func ReplaceByArray(origin string, array []string) string

ReplaceByArray returns a copy of `origin`, which is replaced by a slice in order, case-sensitively.

func ReplaceByMap

func ReplaceByMap(origin string, replaces map[string]string) string

ReplaceByMap returns a copy of `origin`, which is replaced by a map in unordered way, case-sensitively.

func ReplaceI

func ReplaceI(origin, search, replace string, count ...int) string

ReplaceI returns a copy of the string `origin` in which string `search` replaced by `replace` case-insensitively.

func ReplaceIByArray

func ReplaceIByArray(origin string, array []string) string

ReplaceIByArray returns a copy of `origin`, which is replaced by a slice in order, case-insensitively.

func ReplaceIByMap

func ReplaceIByMap(origin string, replaces map[string]string) string

ReplaceIByMap returns a copy of `origin`, which is replaced by a map in unordered way, case-insensitively.

func Reverse

func Reverse(str string) string

Reverse returns a string which is the reverse of `str`.

func RuneLen

func RuneLen(str string) int

RuneLen returns string length of unicode. Deprecated, use LenRune instead.

func SearchArray

func SearchArray(a []string, s string) int

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

func Shuffle(str string) string

Shuffle randomly shuffles a string. It considers parameter `str` as unicode string.

func SimilarText

func SimilarText(first, second string, percent *float64) int

SimilarText calculates the similarity between two strings. See http://php.net/manual/en/function.similar-text.php.

func SnakeCase

func SnakeCase(s string) string

SnakeCase converts a string to snake_case. Deprecated, use CaseSnake instead.

func SnakeFirstUpperCase added in v1.13.3

func SnakeFirstUpperCase(word string, underscore ...string) string

SnakeFirstUpperCase converts a string from RGBCodeMd5 to rgb_code_md5. The length of word should not be too long Deprecated, use CaseSnakeFirstUpper instead.

func SnakeScreamingCase

func SnakeScreamingCase(s string) string

SnakeScreamingCase converts a string to SNAKE_CASE_SCREAMING. Deprecated, use CaseSnakeScreaming instead.

func Soundex

func Soundex(str string) string

Soundex calculates the soundex key of a string. See http://php.net/manual/en/function.soundex.php.

func Split

func Split(str, delimiter string) []string

Split splits string `str` by a string `delimiter`, to an array.

func SplitAndTrim

func SplitAndTrim(str, delimiter string, characterMask ...string) []string

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 SplitAndTrimSpace

func SplitAndTrimSpace(str, delimiter string) []string

SplitAndTrimSpace splits string `str` by a string `delimiter` to an array, and calls TrimSpace to every element of this array. Deprecated, use SplitAndTrim instead.

func Str

func Str(haystack string, needle string) string

Str returns part of `haystack` string starting from and including the first occurrence of `needle` to the end of `haystack`. See http://php.net/manual/en/function.strstr.php.

func StrEx added in v1.15.4

func StrEx(haystack string, needle string) string

StrEx returns part of `haystack` string starting from and excluding the first occurrence of `needle` to the end of `haystack`.

func StrLimit

func StrLimit(str string, length int, suffix ...string) string

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.

func StrLimitRune added in v1.12.2

func StrLimitRune(str string, length int, suffix ...string) string

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.

func StrTill added in v1.15.4

func StrTill(haystack string, needle string) string

StrTill returns part of `haystack` string ending to and including the first occurrence of `needle` from the start of `haystack`.

func StrTillEx added in v1.15.4

func StrTillEx(haystack string, needle string) string

StrTillEx returns part of `haystack` string ending to and excluding the first occurrence of `needle` from the start of `haystack`.

func StripSlashes

func StripSlashes(str string) string

StripSlashes un-quotes a quoted string by AddSlashes.

func SubStr

func SubStr(str string, start int, length ...int) (substr string)

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.

func SubStrRune added in v1.12.2

func SubStrRune(str string, start int, length ...int) (substr string)

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.

func ToLower

func ToLower(s string) string

ToLower returns a copy of the string s with all Unicode letters mapped to their lower case.

func ToUpper

func ToUpper(s string) string

ToUpper returns a copy of the string s with all Unicode letters mapped to their upper case.

func Trim

func Trim(str string, characterMask ...string) string

Trim strips whitespace (or other characters) from the beginning and end of a string. The optional parameter <characterMask> specifies the additional stripped characters.

func TrimAll added in v1.15.4

func TrimAll(str string, characterMask ...string) string

TrimAll trims all characters in string `str`.

func TrimLeft

func TrimLeft(str string, characterMask ...string) string

TrimLeft strips whitespace (or other characters) from the beginning of a string.

func TrimLeftStr

func TrimLeftStr(str string, cut string, count ...int) string

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 TrimRight

func TrimRight(str string, characterMask ...string) string

TrimRight strips whitespace (or other characters) from the end of a string.

func TrimRightStr

func TrimRightStr(str string, cut string, count ...int) string

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

func TrimStr(str string, cut string, count ...int) string

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

func UcFirst(s string) string

UcFirst returns a copy of the string s with the first letter mapped to its upper case.

func UcWords

func UcWords(str string) string

UcWords uppercase the first character of each word in a string.

func WordWrap

func WordWrap(str string, width int, br string) string

WordWrap wraps a string to a given number of characters. TODO: Enable cut parameter, see http://php.net/manual/en/function.wordwrap.php.

Types

This section is empty.

Jump to

Keyboard shortcuts

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