stringx

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: BSD-2-Clause Imports: 10 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 added in v1.3.0

func AppendIfNotEmpty(str string, suffix string) string

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

func CamelToSnake added in v1.4.0

func CamelToSnake(s string) string

CamelToSnake convert camel style ascii str to underscore snake style.

func Capitalize added in v1.3.0

func Capitalize(str string) string

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

func Contains added in v1.8.2

func Contains(str string, sub string) bool

Contains return whether str contains any one of subStrings.

func ContainsAny added in v1.8.2

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

ContainsAny return whether str contains any one of subStrings.

func Copy added in v1.4.0

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

func DeCapitalize(str string) string

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

func Decode added in v1.4.0

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

Decode decode bytes to str using specific encoding

func Encode added in v1.4.0

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

Encode str to bytes using specific encoding

func FirstNonEmpty added in v1.7.0

func FirstNonEmpty(s1, s2 string) string

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

func FromBytes added in v1.8.2

func FromBytes(bytes []byte) string

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

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

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

func PrependIfNotEmpty(str string, prefix string) string

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

func Slice added in v1.7.0

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

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

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 SubstringAfter

func SubstringAfter(str string, sep string) string

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

func SubstringAfterLast

func SubstringAfterLast(str string, sep string) string

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

func SubstringBefore

func SubstringBefore(str string, sep string) string

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

func SubstringBeforeLast

func SubstringBeforeLast(str string, sep string) string

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

func ToBytes added in v1.8.2

func ToBytes(str string) []byte

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

func ValueOf added in v1.4.0

func ValueOf(v interface{}) string

ValueOf return string representation for value

Types

type JoinBuffer added in v1.5.0

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

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

Add adds a new string item to JoinBuffer

func (*JoinBuffer) AddAll added in v1.5.0

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

AddAll add all strings to JoinBuffer

func (*JoinBuffer) AddAllAny added in v1.5.0

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

AddAllAny add all values as string to JoinBuffer

func (*JoinBuffer) AddAllStringer added in v1.5.0

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

AddAllStringer add all Stringer's string value to JoinBuffer

func (*JoinBuffer) AddAny added in v1.5.0

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

AddAny add a new value of any type item to JoinBuffer

func (*JoinBuffer) AddBytes added in v1.5.0

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

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

AddInt add a new int item to JoinBuffer

func (*JoinBuffer) AddInt64 added in v1.5.0

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

AddInt64 add a new int64 item to JoinBuffer

func (*JoinBuffer) AddStringer added in v1.5.0

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

AddStringer add a new stringer item to JoinBuffer

func (*JoinBuffer) AddUint added in v1.5.0

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

AddUint add a new uint item to JoinBuffer

func (*JoinBuffer) AddUint64 added in v1.5.0

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

AddUint64 add a new uint64 item to JoinBuffer

func (*JoinBuffer) Reset added in v1.5.0

func (j *JoinBuffer) Reset() *JoinBuffer

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

func (*JoinBuffer) String added in v1.5.0

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

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

Join joins string items to one string

func (*Joiner) JoinAny added in v1.5.0

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

JoinAny join fmt.Stringer items to one string

func (*Joiner) JoinStringer added in v1.5.0

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

JoinStringer join fmt.Stringer items to one string

func (*Joiner) NewBuffer added in v1.5.0

func (j *Joiner) NewBuffer() *JoinBuffer

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

type Splitter added in v1.2.1

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

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

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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