stringx

package
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2022 License: BSD-2-Clause Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendIfMissing

func AppendIfMissing(str string, suffix string) string

AppendIfMissing return a str end with suffix appended if not has the suffix; otherwise return str it's self

func AppendIfNotEmpty

func AppendIfNotEmpty(str string, suffix string) string

AppendIfNotEmpty return str with suffix if str is not empty; return the origin str otherwise.

func AsBytes

func AsBytes(str string) []byte

AsBytes unsafe convert string to byte slice. This function do not allocate new buffer, reuse the string buffer.

func CamelToSnake

func CamelToSnake(s string) string

CamelToSnake convert camel style ascii str to underscore snake style.

func Capitalize

func Capitalize(str string) string

Capitalize return str with first char of ascii str upper case.

func Contains

func Contains(str string, sub string) bool

Contains return whether str contains any one of subStrings.

func ContainsAny

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

ContainsAny return whether str contains any one of subStrings.

func Copy

func Copy(s string) string

Copy deep copy a string, for reducing large string content memory usage when do substring. This method allocate a new string content byte array, thereby allow the larger string to be released by the garbage collector once it is no longer referenced

func DeCapitalize

func DeCapitalize(str string) string

DeCapitalize return str with first char of ascii str lower case.

func Decode

func Decode(data []byte, enc encoding.Encoding) (string, error)

Decode decode bytes to str using specific encoding

func Encode

func Encode(str string, enc encoding.Encoding) ([]byte, error)

Encode str to bytes using specific encoding

func FirstNonEmpty

func FirstNonEmpty(s1, s2 string, others ...string) string

FirstNonEmpty return first non-empty string; If all string is empty, return empty string.

func PadLeft

func PadLeft(str string, width int, r rune) string

PadLeft pad str to width, with padding rune at left. If str len already equals with or larger than width, return original str.

func PadRight

func PadRight(str string, width int, r rune) string

PadRight pad str to width, with padding rune at right. If str len already equals with or larger than width, return original str.

func PadToCenter

func PadToCenter(str string, width int, r rune) string

PadToCenter pad str to width, with padding rune at left and right. If str len already equals with or larger than width, return original str.

func PrependIfMissing

func PrependIfMissing(str string, prefix string) string

PrependIfMissing return a str start with suffix appended if not has the prefix; otherwise return str it's self

func PrependIfNotEmpty

func PrependIfNotEmpty(str string, prefix string) string

PrependIfNotEmpty return str with prefix if str is not empty; return the origin str otherwise.

func Slice

func Slice(str string, begin, end int) string

Slice return substring from begin index(inclusive) to end index(exclusive). The index for slice can be negative, which means count from end of the string(calculated by str_len + index).

func SliceToEnd

func SliceToEnd(str string, begin int) string

SliceToEnd return substring from begin index(inclusive) to end of string. The index for slice can be negative, which means count from end of the string(calculated by str_len + index).

func SnakeToCamel

func SnakeToCamel(s string, capitalized bool) string

SnakeToCamel convert underscore style ascii str to Camel. The param capitalized determine if first char is converted to uppercase.

func ValueOf

func ValueOf(v interface{}) string

ValueOf return string representation for value

func WrapBytes

func WrapBytes(bytes []byte) string

WrapBytes unsafe convert byte array content to string. This function do not allocate new buffer, reuse the byte array.

Types

type JoinBuffer

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

JoinBuffer is a tool to join string with prefix, suffix, and delimiter.

Usage:

joiner := &JoinBuffer{Separator:",", Prefix:"[", Suffix:"]"}
joiner.Add(str)
s := joiner.String()

func (*JoinBuffer) Add

func (j *JoinBuffer) Add(str string) *JoinBuffer

Add adds a new string item to JoinBuffer

func (*JoinBuffer) AddAll

func (j *JoinBuffer) AddAll(ss ...string) *JoinBuffer

AddAll add all strings to JoinBuffer

func (*JoinBuffer) AddAllAny

func (j *JoinBuffer) AddAllAny(ss ...interface{}) *JoinBuffer

AddAllAny add all values as string to JoinBuffer

func (*JoinBuffer) AddAllStringer

func (j *JoinBuffer) AddAllStringer(ss ...fmt.Stringer) *JoinBuffer

AddAllStringer add all Stringer's string value to JoinBuffer

func (*JoinBuffer) AddAny

func (j *JoinBuffer) AddAny(value interface{}) *JoinBuffer

AddAny add a new value of any type item to JoinBuffer

func (*JoinBuffer) AddBytes

func (j *JoinBuffer) AddBytes(data []byte) *JoinBuffer

AddBytes add new data item to JoinBuffer. The binary data is treated as utf-8 encoded string.

func (*JoinBuffer) AddInt

func (j *JoinBuffer) AddInt(value int) *JoinBuffer

AddInt add a new int item to JoinBuffer

func (*JoinBuffer) AddInt64

func (j *JoinBuffer) AddInt64(value int64) *JoinBuffer

AddInt64 add a new int64 item to JoinBuffer

func (*JoinBuffer) AddStringer

func (j *JoinBuffer) AddStringer(value fmt.Stringer) *JoinBuffer

AddStringer add a new stringer item to JoinBuffer

func (*JoinBuffer) AddUint

func (j *JoinBuffer) AddUint(value uint) *JoinBuffer

AddUint add a new uint item to JoinBuffer

func (*JoinBuffer) AddUint64

func (j *JoinBuffer) AddUint64(value uint64) *JoinBuffer

AddUint64 add a new uint64 item to JoinBuffer

func (*JoinBuffer) Reset

func (j *JoinBuffer) Reset() *JoinBuffer

Reset resets the JoinBuffer to be empty, can be reused.

func (*JoinBuffer) String

func (j *JoinBuffer) String() string

String join all values as string

type Joiner

type Joiner struct {
	Prefix     string // the prefix of joined string result
	Suffix     string // the suffix of joined string result
	Separator  string // the delimiter to join str
	OmitNil    bool   // if skip nil value
	OmitEmpty  bool   // if skip empty string
	NilToEmpty bool   // if trans nil value to empty string
}

Joiner contains join config for joining string. This struct does not have internal state, hence can be reused.

func (*Joiner) Join

func (j *Joiner) Join(strings []string) string

Join joins string items to one string

func (*Joiner) JoinAny

func (j *Joiner) JoinAny(values ...interface{}) string

JoinAny join fmt.Stringer items to one string

func (*Joiner) JoinStringer

func (j *Joiner) JoinStringer(stringers ...fmt.Stringer) string

JoinStringer join fmt.Stringer items to one string

func (*Joiner) NewBuffer

func (j *Joiner) NewBuffer() *JoinBuffer

NewBuffer create and return one new JoinBuffer for join string using the Joiner

type Splitter

type Splitter struct {
	Prefix    string // the prefix of string to remove
	Suffix    string // the suffix of string to remove
	Separator string // the delimiter to split str
}

Splitter is a tool to split string with prefix, suffix, and separator into pieces.

Usage:

splitter := &Splitter{Separator:",", Prefix:"[", Suffix:"]"}
ss := splitter.Split(str)

func (*Splitter) Split

func (sp *Splitter) Split(s string) []string

Split spit a string into pieces, using prefix, suffix, and separator of this Splitter.

type SubStringResult

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

SubStringResult is a result holder for SubstringAfter/SubstringAfterLast/SubstringBefore/SubstringBeforeLast methods.

func SubstringAfter

func SubstringAfter(str string, sep string) SubStringResult

SubstringAfter return sub string after the sep. If str does not contain sep, return empty str.

func SubstringAfterLast

func SubstringAfterLast(str string, sep string) SubStringResult

SubstringAfterLast return sub string after the last sep. If str does not contain sep, return empty str.

func SubstringBefore

func SubstringBefore(str string, sep string) SubStringResult

SubstringBefore return sub string after the sep. If str does not contain sep, return the original str.

func SubstringBeforeLast

func SubstringBeforeLast(str string, sep string) SubStringResult

SubstringBeforeLast return sub string after the last sep. If str does not contain sep, return the original str.

func (SubStringResult) Else

func (r SubStringResult) Else(fallback string) string

Else return the substring result; if substring does not exist, return fallback str.

func (SubStringResult) ElseEmpty

func (r SubStringResult) ElseEmpty() string

ElseEmpty return the substring result; if substring does not exist, return empty str.

func (SubStringResult) ElseOriginal

func (r SubStringResult) ElseOriginal() string

ElseOriginal return the substring result; if substring does not exist, return original str.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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