strings

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelCase

func CamelCase(s string) string

CamelCase returns the CamelCased name. If there is an interior underscore followed by a lower case letter, drop the underscore and convert the letter to upper case. There is a remote possibility of this rewrite causing a name collision, but it's so remote we're prepared to pretend it's nonexistent - since the C++ generator lowercase names, it's extremely unlikely to have two fields with different capitalization. For example, _my_field_name_2 => MyFieldName2, idUser => idUser, IdAccount => idAccount,idUser3 => idUser3

返回字符串的小驼峰样式,例如 _my_field_name_2 => MyFieldName2, idUser => idUser, IdAccount => idAccount,idUser3 => idUser3

Example
fmt.Println(CamelCase("primary_key"))
Output:

primaryKey

func Case

func Case(s string, mode CaseMode) string
Example
fmt.Println(Case("primaryKey", UpperCaseMode))
Output:

PRIMARY_KEY

func MD5

func MD5(vs ...string) string

MD5 is a shortcut of md5.Sum, it'll return a hex string

返回一组字符串联接以后的 MD5 值,将返回 32 位的十六进制字符串

func PascalCase

func PascalCase(s string) string

PascalCase returns the CamelCased name with the first character capitalized.

返回单词的大驼峰样式(将大写字母作为单词分隔符),例如 idUser => IdUser,IdAccount => IdAccount,idUser3 => IdUser3

Example
fmt.Println(PascalCase("primary_key"))
Output:

PrimaryKey

func RandomBytes added in v0.1.2

func RandomBytes(length int) []byte

RandomBytes to generate a random bytes, length < 0 returns nil

生成指定位数的随机字节,length < 0 时返回 nil

func RandomString added in v0.1.2

func RandomString(t RandomStringType, length int) string

RandomString to generate a random string, length < 0 returns empty, notice only works for ASCII string

生成指定位数的安全随机字符串,length < 0 时返回空字符串,注意仅适用于生成 ASCII 字符串

Example
for i := Printable; i < Base64; i++ {
	fmt.Printf(string(RandomString(i, 50)))
}
Output:

0123456789
0123456789

func RegisterRandomStringType added in v0.1.2

func RegisterRandomStringType(t RandomStringType, chars string) bool

RegisterRandomStringType to register a random string type

注册一个随机字符串类型

func SHA224

func SHA224(vs ...string) string

SHA224 is a shortcut of sha256.Sum224

返回一组字符串联接以后的 SHA224 值

func SHA256

func SHA256(vs ...string) string

SHA256 is a shortcut of sha256.Sum256

返回一组字符串联接以后的 SHA256 值

func Sha1

func Sha1(vs ...string) string

Sha1 is a shortcut of sha1.Sum, it'll return a hex string

返回一组字符串联接以后的字符串的 SHA1 值,将返回 32 位的十六进制字符串

func Sha224

func Sha224(vs ...string) string

Sha224 is a shortcut of sha512.Sum512_224

返回一组字符串联接以后的 sha512.Sum512_224 值, 224 位

func Sha256

func Sha256(vs ...string) string

Sha256 is a shortcut of sha512.Sum512_256

返回一组字符串联接以后的 sha512.Sum512_256 值, 256 位

func Sha384

func Sha384(vs ...string) string

Sha384 is a shortcut of sha512.Sum384

返回一组字符串联接以后的 sha512.Sum384 值

func Sha512

func Sha512(vs ...string) string

Sha512 is a shortcut of sha512.Sum

返回一组字符串联接以后的 SHA512 值

func SnakeCase

func SnakeCase(s string) string

SnakeCase 返回单词的蛇形样式(将大写字母作为单词分隔符,前加 _ 并转为小写,首字符除外),其它字符不作额外的处理, 例如 idUser => id_user,IdAccount => id_account

Example
fmt.Println(SnakeCase("primaryKey"))
Output:

primary_key

func Unbracketed

func Unbracketed(name string, brackets ...rune) (string, bool)

Unbracketed attempt to remove the brackets from the name.

Unbracketed("/regexp/", '/') // output: regexp
Unbracketed("<div>", '<', '>') // output: div

去括号,参数 brackets 为括号字符,提供一个字符时,此字符同时作为首尾括号;提供两个字符时,分别作为首尾括号,更多的字符则被忽略

Example
fmt.Println(Unbracketed("<primaryKey>", '<', '>'))
fmt.Println(Unbracketed("/regular expression/", '/'))
Output:

primaryKey true
regular expression true

func Unfold

func Unfold(tags, separator, delimiter string, cast func(string) string) map[string]string

Unfold to split the tags string into a map[string]string using "separator" and "delimiter" as delimiters

使用 separator 和 delimiter 作为分隔符对 tags 字符串进行两次拆分,转换为 map[string]string,注意,未考虑字符转义的情况。

Example
r1 := Unfold("primaryKey;uniqueIndex:idx;size:20", ";", ":", nil)
r2 := Unfold("primaryKey;uniqueIndex:idx;size:20", ";", ":", SnakeCase)
r3 := Unfold("primaryKey;uniqueIndex:idx;size:20", ";", ":", UpperCase)
r4 := Unfold("primaryKey;uniqueIndex:idx;size:20", ";", ":", CamelCase)
r5 := Unfold("primaryKey;uniqueIndex:idx;size:20", ";", ":", PascalCase)
fmt.Println(r1)
fmt.Println(r2)
fmt.Println(r3)
fmt.Println(r4)
fmt.Println(r5)
Output:

map[primaryKey: size:20 uniqueIndex:idx]
map[primary_key: size:20 unique_index:idx]
map[PRIMARY_KEY: SIZE:20 UNIQUE_INDEX:idx]
map[primaryKey: size:20 uniqueIndex:idx]
map[PrimaryKey: Size:20 UniqueIndex:idx]

func Unquote

func Unquote(name string, quotes ...byte) (id string, ok bool, found byte)

Unquote attempt to remove the quotes from the name.

尝试去除名字前后的任意字符(一般用于反撇号,单引号,双引号等), 当名字前后指定字符非成对时,视为不正确,ok 返回 false。 指定多个时依次尝试,但仅执行一次。 found 返回找到的引号字符值,0 为未找到。

Example
fmt.Println(Unquote("'primaryKey'", '\''))
fmt.Println(Unquote("`tag`", '`'))
Output:

primaryKey true 39
tag true 96

func UnquoteRune

func UnquoteRune(name string, quotes ...rune) (id string, ok bool, found rune)

UnquoteRune 尝试去除名字前后的任意字符对(一般用于反撇号,单引号,双引号等), 当名字前后指定字符非成对时,视为不正确,ok 返回 false。 指定多个时依次尝试,但仅执行一次。 found 返回找到的引号字符值,utf8.RuneError 为未找到。

Example
fmt.Println(UnquoteRune("'primaryKey'", '\''))
fmt.Println(UnquoteRune("\"primaryKey\"", '"'))
Output:

primaryKey true 39
primaryKey true 34

func UpperCase

func UpperCase(s string) string

UpperCase 返回单词的大蛇形样式(将大写字母作为单词分隔符,前加 _,首字符除外), 小写字母转为大写字母,其它字符不作处理,例如 idUser => ID_USER,IdAccount => ID_ACCOUNT,idUser3 => ID_USER3

Example
fmt.Println(UpperCase("primaryKey"))
Output:

PRIMARY_KEY

Types

type CaseMode

type CaseMode = int
const (
	Ignored        CaseMode = iota // ignore case style
	SnakeCaseMode                  // snake case style, 蛇形,形如:id_user, id_account
	UpperCaseMode                  // upper snake case style, 大蛇形,形如:ID_USER,ID_ACCOUNT
	CamelCaseMode                  // camel case style, 驼峰,形如:idUser,idAccount
	PascalCaseMode                 // upper camel case style, 大驼峰/Pascal,形如:IdUser,IdAccount
)

type IgnoreCase

type IgnoreCase string

IgnoreCase is case-insensitive string.

func (IgnoreCase) Contains

func (s IgnoreCase) Contains(substring string) bool

func (IgnoreCase) ContainsAny

func (s IgnoreCase) ContainsAny(chars string) bool

func (IgnoreCase) ContainsRune

func (s IgnoreCase) ContainsRune(r rune) bool

func (IgnoreCase) Count

func (s IgnoreCase) Count(substr string) int

func (IgnoreCase) Cut

func (s IgnoreCase) Cut(sep string) (before, after string, found bool)

func (IgnoreCase) CutPrefix

func (s IgnoreCase) CutPrefix(prefix string) (after string, found bool)

func (IgnoreCase) CutSuffix

func (s IgnoreCase) CutSuffix(suffix string) (before string, found bool)

func (IgnoreCase) Equal

func (s IgnoreCase) Equal(t string) bool

func (IgnoreCase) HasPrefix

func (s IgnoreCase) HasPrefix(prefix string) bool

func (IgnoreCase) HasSuffix

func (s IgnoreCase) HasSuffix(suffix string) bool

func (IgnoreCase) Index

func (s IgnoreCase) Index(substring string) int

func (IgnoreCase) IndexAny

func (s IgnoreCase) IndexAny(chars string) int

func (IgnoreCase) IndexRune

func (s IgnoreCase) IndexRune(r rune) int

func (IgnoreCase) LastIndex

func (s IgnoreCase) LastIndex(substring string) int

func (IgnoreCase) LastIndexAny

func (s IgnoreCase) LastIndexAny(chars string) int

func (IgnoreCase) LastIndexRune

func (s IgnoreCase) LastIndexRune(r rune) int

func (IgnoreCase) MemoryUsage

func (s IgnoreCase) MemoryUsage() (sum int64)

MemoryUsage return the memory usage of IgnoreCase

func (IgnoreCase) Replace

func (s IgnoreCase) Replace(old, new string, count int) string

func (IgnoreCase) ReplaceAll

func (s IgnoreCase) ReplaceAll(old, new string) string

func (IgnoreCase) Split

func (s IgnoreCase) Split(sep string) []string

func (IgnoreCase) SplitAfter

func (s IgnoreCase) SplitAfter(sep string) []string

func (IgnoreCase) SplitAfterN

func (s IgnoreCase) SplitAfterN(sep string, n int) []string

func (IgnoreCase) SplitN

func (s IgnoreCase) SplitN(sep string, n int) []string

func (IgnoreCase) ToLower

func (s IgnoreCase) ToLower() string

func (IgnoreCase) ToUpper

func (s IgnoreCase) ToUpper() string

func (IgnoreCase) TrimPrefix

func (s IgnoreCase) TrimPrefix(prefix string) string

func (IgnoreCase) TrimSuffix

func (s IgnoreCase) TrimSuffix(suffix string) string

type RandomStringType added in v0.1.2

type RandomStringType int
const (
	Printable RandomStringType = iota
	Graphical
	NoQuote
	Digits
	AlphabetNumeric
	Alphabet
	AlphabetLowercase
	AlphabetUppercase
	Hex
	HexLower
	HexUpper
	Base64
)

Jump to

Keyboard shortcuts

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