strutil

package
v0.0.0-...-07a9010 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func After

func After(str, ch string) string

After returns the substring after the first occurrence of a specified string in the source string

Example:
	After("hello world", "o") -> " world"

func AfterLast

func AfterLast(str, ch string) string

AfterLast returns the substring after the last occurrence of a specified string in the source string

Example:
	AfterLast("hello world", "o") -> "rld"

func Before

func Before(str, ch string) string

Before returns the string before the first occurrence of ch.

Example:
	Before("no-separator", "-") -> "no"

func BeforeLast

func BeforeLast(str, ch string) string

BeforeLast returns the string before the last occurrence of ch.

Example:
	BeforeLast("abcabc", "c") -> "abcab"

func BytesToString

func BytesToString(bs []byte) string

BytesToString converts the byte slice to string without memory alloc.

func CamelCase

func CamelCase(str string) string

CamelCase converts string to camelCase string. Non letters and numbers will be ignored.

Example:
	"!@#" -> ""
	"&FOO:BAR$BAZ" -> "fooBarBaz"
	"hello_world" -> "helloWorld"

func Capitalize

func Capitalize(str string) string

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

Example:
	"hello" -> "Hello"
	"GoLang" -> "Golang"

func Concat

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 unsure about the length of string to be concatenated, length set to 0 / negetive number.

func ContainChinese

func ContainChinese(str string) bool

ContainChinese check if the string contains Chinese characters.

func ContainLetter

func ContainLetter[T string | []byte](dat T) bool

ContainLetter checks if the string contains any letters (a-zA-Z).

func ContainLower

func ContainLower(str string) bool

ContainLower checks if the string contains any lower letters (a-z).

func ContainNumber

func ContainNumber[T string | []byte](dat T) bool

ContainNumber checks if the string contains any numbers (0-9).

func ContainUpper

func ContainUpper(str string) bool

ContainUpper checks if the string contains any upper letters (A-Z).

func ContainsAll

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

ContainsAll checks if the string contains all substrings.

func ContainsAny

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

ContainsAny checks if the string contains any substring.

func Ellipsis

func Ellipsis(str string, size int) string

Ellipsis returns a string with ellipsis if the string is longer than the specified size.

func HammingDistance

func HammingDistance(str1, str2 string) (int, error)

HammingDistance returns the Hamming distance between two strings.

func HasPrefixAny

func HasPrefixAny(str string, prefixs ...string) bool

HasPrefixAny checks if the string has any of the given prefixes.

func HasSuffixAny

func HasSuffixAny(str string, suffixs ...string) bool

HasSuffixAny checks if the string has any of the given suffixes.

func HideString

func HideString(src string, beg, end int, hideChar string) string

HideString hides the string between beg and end with the hideChar.

Example:
	HideString({"hello world", 3, 7, "*") -> "hel****orld"

func In

func In(dst string, src []string) bool

In returns true if the dst is in the src.

func IndexOffset

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

IndexOffset return the index of the first occurrence of the substring in the string after offset, or -1 if not found.

func IsASCII

func IsASCII(str string) bool

IsACII checks if the string is all ASCII characters.

func IsAllLower

func IsAllLower(str string) bool

IsAllLower checks if the string is all lower letters (a-z).

func IsAllUpper

func IsAllUpper(str string) bool

IsAllUpper checks if the string is all upper letters (A-Z).

func IsAlpha

func IsAlpha(str string) bool

IsAlpha checks if the string contains only letters (a-zA-Z).

func IsAmericanExpress

func IsAmericanExpress(v string) bool

IsAmericanExpress check if a give string is a valid american expression card nubmer or not.

func IsBase64

func IsBase64(base64 string) bool

IsBase64 check if the string is base64 string.

func IsBase64URL

func IsBase64URL(v string) bool

IsBase64URL check if the string is a valid RUL-safe Base64 encoded string.

func IsBin

func IsBin(dat string) bool

IsBin check if the string is a valid binary value or not.

func IsChinaUnionPay

func IsChinaUnionPay(v string) bool

IsChinaUnionPay check if a give string is a valid china union pay nubmer or not.

func IsChineseIDNum

func IsChineseIDNum(id string) bool

IsChineseIDNum check if the string is a valid Chinese ID card number.

func IsChineseMobile

func IsChineseMobile(str string) bool

IsChineseMobile check if the string is a valid Chinese mobile phone number.

func IsChinesePhone

func IsChinesePhone(phone string) bool

IsChinesePhone check if the string is chinese phone number. Valid chinese phone is xxx-xxxxxxxx or xxxx-xxxxxxx.

func IsCreditCard

func IsCreditCard(creditCart string) bool

IsCreditCard check if the string is credit card.

func IsDNS

func IsDNS(str string) bool

IsDNS check if the string is a valid DNS name.

func IsEmail

func IsEmail(str string) bool

IsEmail check if the string is a valid email address.

func IsEmptyString

func IsEmptyString(str string) bool

IsEmptyString check if the string is empty.

func IsFloat

func IsFloat(v any) bool

IsFloat check if value is a float (float32, float64) or not.

func IsFloatStr

func IsFloatStr(str string) bool

IsFloatStr check if the string can convert to a float.

func IsGBK

func IsGBK(data []byte) bool

IsGBK check if data encoding is gbk Note: this function is implemented by whether double bytes fall within the encoding range of gbk, while each byte of utf-8 encoding format falls within the encoding range of gbk. Therefore, utf8.valid() should be called first to check whether it is not utf-8 encoding, and then call IsGBK() to check gbk encoding. like below *

data := []byte("你好")
if utf8.Valid(data) {
	fmt.Println("data encoding is utf-8")
}else if(IsGBK(data)) {
	fmt.Println("data encoding is GBK")
}
fmt.Println("data encoding is unknown")

*

func IsHex

func IsHex(dat string) bool

IsHex check if the string is a valid hexadecimal value or not.

func IsIP

func IsIP(str string) bool

IsIP check if the string is a valid IP address.

func IsIPV4

func IsIPV4(str string) bool

IsIPV4 check if the string is a valid IPv4 address.

func IsIPV6

func IsIPV6(str string) bool

IsIPV6 check if the string is a valid IPv6 address.

func IsInt

func IsInt(v any) bool

IsInt check if value is a integer (int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr) or not.

func IsIntStr

func IsIntStr(str string) bool

IsIntStr check if the string can convert to a integer.

func IsJSON

func IsJSON(str string) bool

IsJSON checks if the string is a valid JSON string.

func IsJWT

func IsJWT(str string) bool

IsJWT check if a string is a valid JSON Web Token (JWT).

func IsMasterCard

func IsMasterCard(v string) bool

IsMasterCard check if a give string is a valid master card nubmer or not.

func IsNotSpace

func IsNotSpace(str string) bool

IsNotSpace checks if the string is not whitespace, empty or not.

func IsNumber

func IsNumber(v any) bool

IsNumber check if value is a number (integer, float) or not.

func IsNumberStr

func IsNumberStr(str string) bool

IsNumberStr check if the string can convert to a number.

func IsPort

func IsPort(str string) bool

IsPort check if the string is a valid port number [1 ~ 65536).

func IsPrint

func IsPrint(str string) bool

IsPrint checks if the string contains only printable characters.

func IsRegexMatch

func IsRegexMatch(str, regex string) bool

IsRegexMatch check if the string match the regexp.

func IsSpace

func IsSpace(str string) bool

IsSpace checks if the string is whitespace, empty or not.

Example:
	"" -> true
	" \t\n\r" -> true

func IsString

func IsString(val any) bool

IsString checks if the val data type is string or not.

func IsStrongPassword

func IsStrongPassword(str string, length int) bool

IsStrongPassword check if the string is strong password, if len(password) is less than the length param, return false.

Strong password: alpha(lower+upper) + number + special chars(!@#$%^&*()?><).

func IsURL

func IsURL(str string) bool

IsURL check if the string is a valid URL.

func IsUnionPay

func IsUnionPay(v string) bool

IsUnionPay check if a give string is a valid union pay nubmer or not.

func IsVisa

func IsVisa(str string) bool

IsVisa check if a string is a valid Visa card number.

func IsWeakPassword

func IsWeakPassword(str string) bool

IsWeakPassword check if the string is weak password.

Weak password: only letter or only number or letter+number.

func IsZeroValue

func IsZeroValue(val any) bool

IsZeroValue check if the value is zero value.

func KebabCase

func KebabCase(str string) string

KebabCase converts string to kebab-case string, non letters and numbers will be ignored.

Example:
	"hello_world" -> "hello-world"
	"&FOO:BAR$BAZ" -> "foo-bar-baz"

func LowerFirst

func LowerFirst(str string) string

LowerFirst converts first character of string to lower case.

Example:
	"HELLO WORLD" -> "hELLO WORLD"

func Pad

func Pad(src string, size int, pad string) string

Pad pads the src to the left side and the right side with pad string until the src is size long.

func PadLeft

func PadLeft(src string, size int, pad string) string

PadLeft pads the src to the left side with pad string until the src is size long.

func PadRight

func PadRight(src string, size int, pad string) string

PadRight pads the src to the right side with pad string until the src is size long.

func RegexMatchAllGroups

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

RegexMatchAll returns all matches of the pattern in the string.

Example
	("abc123",`(\w)(\w)(\w)`) -> [][]string{{"abc", "a", "b", "c"}, {"123", "1", "2", "3"}}

func RemoveNonPrintable

func RemoveNonPrintable(str string) string

RemoveNonPrintable removes all non-printable characters from the string.

func RemoveWhiteSpace

func RemoveWhiteSpace(str string, rmAll bool) string

RemoveWhiteSpace removes withespace from the string. When rmAll is true, all whitespaces are removed, otherwise only consective whitespaces are removed.

Example:
	RemoveWhiteSpace("  hello   world  ", false) -> "hello world"
	RemoveWitheSpace("  hello   world  ", true) -> "helloworld"

func ReplaceWithMap

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

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

func Reverse

func Reverse(str string) string

Reverse reverses the string.

func Rotate

func Rotate(str string, shift int) string

Rotate rotates the string by the specified number of characters.

func Shuffle

func Shuffle(str string) (string, error)

Shuffle returns a shuffled string or error.

func SnakeCase

func SnakeCase(str string) string

SnakeCase converts string to snake_case string, non letters and numbers will be ignored.

Example:
	"hello_world" -> "hello_world"
	"&FOO:BAR$BAZ" -> "foo_bar_baz"

func SplitAndTrim

func SplitAndTrim(str, delimeter string, cutset ...string) (vStr []string)

SplitAndTrim splits the string by the delimeter and trims the result.

Example:
	SplitAndTrim("a,b,cHello,d,eWorld,f", ",", "abc") -> []string{"Hello", "d", "eWorld", "f"}

func StringToBytes

func StringToBytes(str string) []byte

StringToBytes converts the string to byte slice without memory alloc.

func SubInBetween

func SubInBetween(str, beg, end string) string

SubInBetween returns the substring between the beg and end.

func SubString

func SubString(src string, begin int, size int) string

SubString returns a substring of the specified size from begin.

func Trim

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

func UnWarp

func UnWarp(str, sWarp string) string

UnWarp unwraps the string with the given string.

Example:
	UnWarp("*hello*", "*") -> "hello"
	UnWarp("abcdefabc", "abc") -> "def"

func UpperFirst

func UpperFirst(str string) string

UpperFirst converts first character of string to upper case.

Example:
	"hello" -> "Hello"

func UpperKebabCase

func UpperKebabCase(str string) string

UpperKebabCase converts string to upper KEBAB-CASE string, non letters and numbers will be ignored.

Example:
	"Hello World" -> "HELLO-WORLD"
	"!@#" -> ""

func UpperSnakeCase

func UpperSnakeCase(str string) string

UpperSnakeCase converts string to upper SNAKE_CASE string, non letters and numbers will be ignored.

Example:
	"Hello World" -> "HELLO_WORLD"
	"!" -> ""

func Warp

func Warp(str, sWarp string) string

Warp wraps the string with the given string.

Example:
	Warp("hello", "*") -> "*hello*"

Types

This section is empty.

Jump to

Keyboard shortcuts

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