Documentation ¶
Overview ¶
Package strutil implements some functions to manipulate string.
Index ¶
- Variables
- func After(s, char string) string
- func AfterLast(s, char string) string
- func Before(s, char string) string
- func BeforeLast(s, char string) string
- func BytesToString(bytes []byte) string
- func CamelCase(s string) string
- func Capitalize(s string) string
- func Concat(length int, str ...string) string
- func ContainsAll(str string, substrs []string) bool
- func ContainsAny(str string, substrs []string) bool
- func Ellipsis(str string, length int) string
- func HammingDistance(a, b string) (int, error)
- func HasPrefixAny(str string, prefixes []string) bool
- func HasSuffixAny(str string, suffixes []string) bool
- func HideString(origin string, start, end int, replaceChar string) string
- func IndexOffset(str string, substr string, idxFrom int) int
- func IsBlank(str string) bool
- func IsString(v interface{}) bool
- func KebabCase(s string) string
- func LowerFirst(s string) string
- func Pad(source string, size int, padStr string) string
- func PadEnd(source string, size int, padStr string) string
- func PadStart(source string, size int, padStr string) string
- func RegexMatchAllGroups(pattern, str string) [][]string
- func RemoveNonPrintable(str string) string
- func RemoveWhiteSpace(str string, repalceAll bool) string
- func ReplaceWithMap(str string, replaces map[string]string) string
- func Reverse(s string) string
- func Rotate(str string, shift int) string
- func Shuffle(str string) string
- func SnakeCase(s string) string
- func SplitAndTrim(str, delimiter string, characterMask ...string) []string
- func SplitEx(s, sep string, removeEmptyString bool) []string
- func SplitWords(s string) []string
- func StringToBytes(str string) (b []byte)
- func SubInBetween(str string, start string, end string) string
- func TemplateReplace(template string, data map[string]string) string
- func Trim(str string, characterMask ...string) string
- func Unwrap(str string, wrapToken string) string
- func UpperFirst(s string) string
- func UpperKebabCase(s string) string
- func UpperSnakeCase(s string) string
- func WordCount(s string) int
- func Wrap(str string, wrapWith string) string
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultTrimChars are the characters which are stripped by Trim* functions in default. DefaultTrimChars = string([]byte{ '\t', '\v', '\n', '\r', '\f', ' ', 0x00, 0x85, 0xA0, }) )
Functions ¶
func BeforeLast ¶
BeforeLast create substring in source string before position when char last appear
func BytesToString ¶ added in v1.3.8
BytesToString converts a byte slice to string without a memory allocation. Play: todo
func CamelCase ¶
CamelCase covert string to camelCase string. non letters and numbers will be ignored eg. "Foo-#1😄$_%^&*(1bar" => "foo11Bar"
func Capitalize ¶
Capitalize converts the first character of a string to upper case and the remaining to lower case.
func Concat ¶ added in v1.4.4
Concat uses the strings.Builder to concatenate the input strings.
- `length` is the expected length of the concatenated string.
- if you are unsure about the length of the string to be concatenated, please pass 0 or a negative number.
func ContainsAll ¶ added in v1.3.9
ContainsAll return true if target string contains all the substrs.
func ContainsAny ¶ added in v1.3.9
ContainsAny return true if target string contains any one of the substrs.
func Ellipsis ¶ added in v1.4.4
Ellipsis truncates a string to a specified length and appends an ellipsis.
func HammingDistance ¶ added in v1.4.3
HammingDistance calculates the Hamming distance between two strings. The Hamming distance is the number of positions at which the corresponding symbols are different. This func returns an error if the input strings are of unequal lengths.
func HasPrefixAny ¶ added in v1.3.8
HasPrefixAny check if a string starts with any of an array of specified strings.
func HasSuffixAny ¶ added in v1.3.8
HasSuffixAny check if a string ends with any of an array of specified strings.
func HideString ¶ added in v1.3.9
HideString hide some chars in source string with param `replaceChar`. replace range is origin[start : end]. [start, end)
func IndexOffset ¶ added in v1.3.8
IndexOffset returns the index of the first instance of substr in string after offsetting the string by `idxFrom`, or -1 if substr is not present in string.
func IsString ¶
func IsString(v interface{}) bool
IsString check if the value data type is string or not.
func KebabCase ¶
KebabCase covert string to kebab-case non letters and numbers will be ignored eg. "Foo-#1😄$_%^&*(1bar" => "foo-1-1-bar"
func LowerFirst ¶
LowerFirst converts the first character of string to lower case.
func Pad ¶ added in v1.3.7
PadStart pads string on the left and right side if it's shorter than size. Padding characters are truncated if they exceed size.
func PadEnd ¶
PadEnd pads string on the right side if it's shorter than size. Padding characters are truncated if they exceed size.
func PadStart ¶
PadStart pads string on the left side if it's shorter than size. Padding characters are truncated if they exceed size.
func RegexMatchAllGroups ¶ added in v1.4.4
RegexMatchAllGroups Matches all subgroups in a string using a regular expression and returns the result.
func RemoveNonPrintable ¶ added in v1.3.8
RemoveNonPrintable remove non-printable characters from a string.
func RemoveWhiteSpace ¶ added in v1.4.0
RemoveWhiteSpace remove whitespace characters from a string. when set repalceAll is true removes all whitespace, false only replaces consecutive whitespace characters with one space.
func ReplaceWithMap ¶ added in v1.3.9
ReplaceWithMap returns a copy of `str`, which is replaced by a map in unordered way, case-sensitively.
func Reverse ¶ added in v1.3.2
Reverse return string whose char order is reversed to the given string
func SnakeCase ¶
SnakeCase covert string to snake_case non letters and numbers will be ignored eg. "Foo-#1😄$_%^&*(1bar" => "foo_1_1_bar"
func SplitAndTrim ¶ added in v1.3.9
SplitAndTrim splits string `str` by a string `delimiter` to a slice, and calls Trim to every element of this slice. It ignores the elements which are empty after Trim.
func SplitEx ¶ added in v1.3.0
SplitEx split a given string whether the result contains empty string
func SplitWords ¶ added in v1.3.7
SplitWords splits a string into words, word only contains alphabetic characters.
func StringToBytes ¶ added in v1.3.8
StringToBytes converts a string to byte slice without a memory allocation.
func SubInBetween ¶ added in v1.4.3
SubInBetween return substring between the start and end position(excluded) of source string.
func TemplateReplace ¶ added in v1.4.4
TemplateReplace replaces the placeholders in the template string with the corresponding values in the data map. The placeholders are enclosed in curly braces, e.g. {key}. for example, the template string is "Hello, {name}!", and the data map is {"name": "world"}, the result will be "Hello, world!".
func Trim ¶ added in v1.3.9
Trim strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.
func UpperFirst ¶ added in v1.2.2
UpperFirst converts the first character of string to upper case.
func UpperKebabCase ¶ added in v1.3.5
UpperKebabCase covert string to upper KEBAB-CASE non letters and numbers will be ignored eg. "Foo-#1😄$_%^&*(1bar" => "FOO-1-1-BAR"
func UpperSnakeCase ¶ added in v1.3.5
UpperSnakeCase covert string to upper SNAKE_CASE non letters and numbers will be ignored eg. "Foo-#1😄$_%^&*(1bar" => "FOO_1_1_BAR"
Types ¶
This section is empty.