strutil

package
v1.4.4 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: MIT Imports: 8 Imported by: 11

Documentation

Overview

Package strutil implements some functions to manipulate string.

Index

Constants

This section is empty.

Variables

View Source
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 After

func After(s, char string) string

After create substring in source string after position when char first appear

func AfterLast

func AfterLast(s, char string) string

AfterLast create substring in source string after position when char last appear

func Before

func Before(s, char string) string

Before create substring in source string before position when char first appear

func BeforeLast

func BeforeLast(s, char string) string

BeforeLast create substring in source string before position when char last appear

func BytesToString added in v1.3.8

func BytesToString(bytes []byte) string

BytesToString converts a byte slice to string without a memory allocation. Play: todo

func CamelCase

func CamelCase(s string) string

CamelCase covert string to camelCase string. non letters and numbers will be ignored eg. "Foo-#1😄$_%^&*(1bar" => "foo11Bar"

func Capitalize

func Capitalize(s string) string

Capitalize converts the first character of a string to upper case and the remaining to lower case.

func Concat added in v1.4.4

func Concat(length int, str ...string) string

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

func ContainsAll(str string, substrs []string) bool

ContainsAll return true if target string contains all the substrs.

func ContainsAny added in v1.3.9

func ContainsAny(str string, substrs []string) bool

ContainsAny return true if target string contains any one of the substrs.

func Ellipsis added in v1.4.4

func Ellipsis(str string, length int) string

Ellipsis truncates a string to a specified length and appends an ellipsis.

func HammingDistance added in v1.4.3

func HammingDistance(a, b string) (int, error)

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

func HasPrefixAny(str string, prefixes []string) bool

HasPrefixAny check if a string starts with any of an array of specified strings.

func HasSuffixAny added in v1.3.8

func HasSuffixAny(str string, suffixes []string) bool

HasSuffixAny check if a string ends with any of an array of specified strings.

func HideString added in v1.3.9

func HideString(origin string, start, end int, replaceChar string) string

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

func IndexOffset(str string, substr string, idxFrom int) int

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

func IsBlank(str string) bool

IsBlank checks if a string is whitespace, empty.

func IsString

func IsString(v interface{}) bool

IsString check if the value data type is string or not.

func KebabCase

func KebabCase(s string) string

KebabCase covert string to kebab-case non letters and numbers will be ignored eg. "Foo-#1😄$_%^&*(1bar" => "foo-1-1-bar"

func LowerFirst

func LowerFirst(s string) string

LowerFirst converts the first character of string to lower case.

func Pad added in v1.3.7

func Pad(source string, size int, padStr string) string

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

func PadEnd(source string, size int, padStr string) string

PadEnd pads string on the right side if it's shorter than size. Padding characters are truncated if they exceed size.

func PadStart

func PadStart(source string, size int, padStr string) string

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

func RegexMatchAllGroups(pattern, str string) [][]string

RegexMatchAllGroups Matches all subgroups in a string using a regular expression and returns the result.

func RemoveNonPrintable added in v1.3.8

func RemoveNonPrintable(str string) string

RemoveNonPrintable remove non-printable characters from a string.

func RemoveWhiteSpace added in v1.4.0

func RemoveWhiteSpace(str string, repalceAll bool) string

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

func ReplaceWithMap(str string, replaces map[string]string) string

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

func Reverse added in v1.3.2

func Reverse(s string) string

Reverse return string whose char order is reversed to the given string

func Rotate added in v1.4.4

func Rotate(str string, shift int) string

Rotate rotates the string by the specified number of characters.

func Shuffle added in v1.4.4

func Shuffle(str string) string

Shuffle the order of characters of given string.

func SnakeCase

func SnakeCase(s string) string

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

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

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

func SplitEx(s, sep string, removeEmptyString bool) []string

SplitEx split a given string whether the result contains empty string

func SplitWords added in v1.3.7

func SplitWords(s string) []string

SplitWords splits a string into words, word only contains alphabetic characters.

func StringToBytes added in v1.3.8

func StringToBytes(str string) (b []byte)

StringToBytes converts a string to byte slice without a memory allocation.

func SubInBetween added in v1.4.3

func SubInBetween(str string, start string, end string) string

SubInBetween return substring between the start and end position(excluded) of source string.

func TemplateReplace added in v1.4.4

func TemplateReplace(template string, data map[string]string) string

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

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

func Unwrap(str string, wrapToken string) string

Unwrap a given string from anther string. will change str value

func UpperFirst added in v1.2.2

func UpperFirst(s string) string

UpperFirst converts the first character of string to upper case.

func UpperKebabCase added in v1.3.5

func UpperKebabCase(s string) string

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

func UpperSnakeCase(s string) string

UpperSnakeCase covert string to upper SNAKE_CASE non letters and numbers will be ignored eg. "Foo-#1😄$_%^&*(1bar" => "FOO_1_1_BAR"

func WordCount added in v1.3.7

func WordCount(s string) int

WordCount return the number of meaningful word, word only contains alphabetic characters.

func Wrap added in v1.1.7

func Wrap(str string, wrapWith string) string

Wrap a string with another string.

Types

This section is empty.

Jump to

Keyboard shortcuts

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