strutil

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2020 License: MIT Imports: 16 Imported by: 125

README

String Util

This is an go string operate util package.

Install

go get github.com/gookit/goutil/dump

Usage

ss := strutil.ToArray("a,b,c", ",")
// Output: []string{"a", "b", "c"}

ints, err := strutil.ToIntSlice("1,2,3")
// Output: []int{1, 2, 3}

Functions

func B64Encode(str string) string
func Bool(s string) (bool, error)
func Camel(s string, sep ...string) string
func CamelCase(s string, sep ...string) string
func FilterEmail(s string) string
func GenMd5(src interface{}) string
func LowerFirst(s string) string
func Lowercase(s string) string
func Md5(src interface{}) string
func MustBool(s string) bool
func MustString(in interface{}) string
func PadLeft(s, pad string, length int) string
func PadRight(s, pad string, length int) string
func Padding(s, pad string, length int, pos uint8) string
func PrettyJSON(v interface{}) (string, error)
func RandomBytes(length int) ([]byte, error)
func RandomString(length int) (string, error)
func RenderTemplate(input string, data interface{}, fns template.FuncMap, isFile ...bool) string
func Repeat(s string, times int) string
func RepeatRune(char rune, times int) (chars []rune)
func Replaces(str string, pairs map[string]string) string
func Similarity(s, t string, rate float32) (float32, bool)
func Snake(s string, sep ...string) string
func SnakeCase(s string, sep ...string) string
func Split(s, sep string) (ss []string)
func String(val interface{}) (string, error)
func Substr(s string, pos, length int) string
func ToArray(s string, sep ...string) []string
func ToBool(s string) (bool, error)
func ToIntSlice(s string, sep ...string) (ints []int, err error)
func ToInts(s string, sep ...string) ([]int, error)
func ToSlice(s string, sep ...string) []string
func ToString(val interface{}) (str string, err error)
func ToTime(s string, layouts ...string) (t time.Time, err error)
func Trim(s string, cutSet ...string) string
func TrimLeft(s string, cutSet ...string) string
func TrimRight(s string, cutSet ...string) string
func URLDecode(s string) string
func URLEncode(s string) string
func UpperFirst(s string) string
func UpperWord(s string) string
func Uppercase(s string) string

Documentation

Index

Constants

View Source
const (
	AlphaBet  = "abcdefghijklmnopqrstuvwxyz"
	AlphaNum  = "abcdefghijklmnopqrstuvwxyz0123456789"
	AlphaNum2 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
View Source
const (
	PosLeft uint8 = iota
	PosRight
)

Position for padding string

Variables

View Source
var (
	// EscapeJS escape javascript string
	EscapeJS = template.JSEscapeString
	// EscapeHTML escape html string
	EscapeHTML = template.HTMLEscapeString
)
View Source
var (
	Lower = strings.ToLower
	Upper = strings.ToUpper
	Title = strings.ToTitle
)

Some alias methods.

View Source
var (
	DefMinInt = 1000
	DefMaxInt = 9999
)
View Source
var (
	ErrConvertFail  = errors.New("convert data type is failure")
	ErrInvalidParam = errors.New("invalid input parameter")
)

Functions

func B64Encode added in v0.2.2

func B64Encode(str string) string

B64Encode base64 encode

func Base64 added in v0.2.3

func Base64(str string) string

Base64 base64 encode

func Bool added in v0.2.2

func Bool(s string) (bool, error)

Bool parse string to bool

func Camel added in v0.2.2

func Camel(s string, sep ...string) string

Camel alias of the CamelCase

func CamelCase added in v0.2.2

func CamelCase(s string, sep ...string) string

CamelCase convert string to camel case. Support:

"range_price" -> "rangePrice"
"range price" -> "rangePrice"
"range-price" -> "rangePrice"

func FilterEmail added in v0.2.2

func FilterEmail(s string) string

FilterEmail filter email, clear invalid chars.

func GenMd5

func GenMd5(src interface{}) string

GenMd5 Generate a 32-bit md5 string

func Int added in v0.2.8

func Int(s string) (int, error)

Int convert string to int

func IsAlphabet added in v0.2.13

func IsAlphabet(char uint8) bool

IsAlphabet char

func LowerFirst

func LowerFirst(s string) string

LowerFirst lower first char

func Lowercase added in v0.2.2

func Lowercase(s string) string

Lowercase alias of the strings.ToLower()

func Md5 added in v0.2.2

func Md5(src interface{}) string

Md5 Generate a 32-bit md5 string

func MicroTimeHexID added in v0.3.6

func MicroTimeHexID() string

MicroTimeHexID generate. return like: 5b5f0588af1761ad3(len: 16-17)

func MicroTimeID added in v0.3.6

func MicroTimeID() string

MicroTimeID generate. return like: 16074145697981929446(len: 20)

func MustBool added in v0.2.2

func MustBool(s string) bool

MustBool convert.

func MustInt added in v0.2.8

func MustInt(s string) int

ToInt convert string to int

func MustString added in v0.2.2

func MustString(in interface{}) string

MustString convert value to string

func PadLeft

func PadLeft(s, pad string, length int) string

PadLeft a string.

func PadRight

func PadRight(s, pad string, length int) string

PadRight a string.

func Padding

func Padding(s, pad string, length int, pos uint8) string

Padding a string.

func PrettyJSON

func PrettyJSON(v interface{}) (string, error)

PrettyJSON get pretty Json string Deprecated

please use fmtutil.PrettyJSON() or jsonutil.Pretty() instead it

func RandomBytes

func RandomBytes(length int) ([]byte, error)

RandomBytes generate

func RandomChars added in v0.3.6

func RandomChars(ln int) string

RandomChars generate give length random chars at `a-z`

func RandomCharsV2 added in v0.3.6

func RandomCharsV2(ln int) string

RandomCharsV2 generate give length random chars in `0-9a-z`

func RandomCharsV3 added in v0.3.6

func RandomCharsV3(ln int) string

RandomCharsV3 generate give length random chars in `0-9a-zA-Z`

func RandomString

func RandomString(length int) (string, error)

RandomString generate. Example:

	// this will give us a 44 byte, base64 encoded output
	token, err := RandomString(32)
	if err != nil {
    // Serve an appropriately vague error to the
    // user, but log the details internally.
	}

func RenderTemplate

func RenderTemplate(input string, data interface{}, fns template.FuncMap, isFile ...bool) string

RenderTemplate render text template

func RenderText added in v0.2.4

func RenderText(input string, data interface{}, fns template.FuncMap, isFile ...bool) string

RenderText render text template

func Repeat

func Repeat(s string, times int) string

Repeat repeat a string

func RepeatRune

func RepeatRune(char rune, times int) (chars []rune)

RepeatRune repeat a rune char.

func Replaces

func Replaces(str string, pairs map[string]string) string

Replaces replace multi strings

pairs: {old1: new1, old2: new2, ...}

Can also use:

strings.NewReplacer("old1", "new1", "old2", "new2").Replace(str)

func Similarity

func Similarity(s, t string, rate float32) (float32, bool)

Similarity calc for two string. Usage:

rate, ok := Similarity("hello", "he")

func Snake added in v0.2.2

func Snake(s string, sep ...string) string

Snake alias of the SnakeCase

func SnakeCase added in v0.2.2

func SnakeCase(s string, sep ...string) string

SnakeCase convert. eg "RangePrice" -> "range_price"

func Split

func Split(s, sep string) (ss []string)

Split string to slice. will clear empty string node.

func String added in v0.2.2

func String(val interface{}) (string, error)

String convert val to string

func Substr

func Substr(s string, pos, length int) string

Substr for a string.

func ToArray added in v0.2.2

func ToArray(s string, sep ...string) []string

ToArray alias of the ToSlice()

func ToBool added in v0.2.2

func ToBool(s string) (bool, error)

ToBool convert string to bool

func ToInt added in v0.2.8

func ToInt(s string) (int, error)

ToInt convert string to int

func ToIntSlice added in v0.2.2

func ToIntSlice(s string, sep ...string) (ints []int, err error)

ToIntSlice split string to slice and convert item to int.

func ToInts added in v0.2.2

func ToInts(s string, sep ...string) ([]int, error)

ToInts alias of the ToIntSlice()

func ToSlice added in v0.2.2

func ToSlice(s string, sep ...string) []string

ToSlice split string to array.

func ToString added in v0.2.2

func ToString(val interface{}) (str string, err error)

ToString convert value to string

func ToTime added in v0.2.2

func ToTime(s string, layouts ...string) (t time.Time, err error)

ToTime convert date string to time.Time

func Trim added in v0.2.2

func Trim(s string, cutSet ...string) string

Trim string

func TrimLeft added in v0.2.2

func TrimLeft(s string, cutSet ...string) string

TrimLeft char in the string.

func TrimRight added in v0.2.2

func TrimRight(s string, cutSet ...string) string

TrimRight char in the string.

func URLDecode added in v0.2.2

func URLDecode(s string) string

URLDecode decode url string.

func URLEncode added in v0.2.2

func URLEncode(s string) string

URLEncode encode url string.

func UpperFirst

func UpperFirst(s string) string

UpperFirst upper first char

func UpperWord

func UpperWord(s string) string

UpperWord Change the first character of each word to uppercase

func Uppercase added in v0.2.2

func Uppercase(s string) string

Uppercase alias of the strings.ToUpper()

Types

type SimilarComparator

type SimilarComparator struct {
	// contains filtered or unexported fields
}

SimilarComparator definition links:

https://github.com/mkideal/cli/blob/master/fuzzy.go

func NewComparator

func NewComparator(src, dst string) *SimilarComparator

NewComparator create

func (*SimilarComparator) Similar

func (c *SimilarComparator) Similar(minDifferRate float32) (float32, bool)

Similar by minDifferRate Usage:

c := NewComparator("hello", "he")
rate, ok :c.Similar(0.3)

Jump to

Keyboard shortcuts

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