Documentation
¶
Index ¶
- func AddSlashes(str string) string
- func AddSlashesCustom(str string, escapeChars string) string
- func After(str, needle string) string
- func AfterLast(str, needle string) string
- func Append(values ...string) string
- func Base32ExtendedDecode(data []byte) (text []byte, err error)
- func Base32ExtendedEncode(data []byte) (text []byte, err error)
- func Base64Decode(text []byte) (data []byte, err error)
- func Base64Encode(data []byte) (text []byte, err error)
- func Basename(p string, suffix ...string) string
- func BcryptHashCompare(str string, hash string) bool
- func Before(str, needle string) string
- func BeforeLast(str, needle string) string
- func Between(str string, startNeedle string, endNeedle string) (result string, found bool)
- func BetweenFirst(str, start, end string) string
- func CharAt(str string, index int) string
- func ChopEnd(str string, needle string, more ...string) string
- func ChopStart(str string, needle string, more ...string) string
- func Contains(str string, values ...string) bool
- func ContainsAll(str string, values ...string) bool
- func ContainsAnyChar(str string, charset string) bool
- func ContainsOnly(str string, charset string) bool
- func CurrencySymbol(currencyCode string, htmlEntity ...bool) string
- func Dirname(p string, levels ...int) string
- func EndsWith(str string, values ...string) bool
- func Exactly(str, value string) bool
- func Excerpt(str, phrase string, options ...ExcerptOption) string
- func Explode(str, delimiter string, limit ...int) []string
- func Finish(str, suffix string) string
- func Headline(in string) string
- func IntToBase32(num int) string
- func IntToBase36(num int) string
- func Is(str string, patterns ...string) bool
- func IsAscii(str string) bool
- func IsEmpty(str string) bool
- func IsMap(str string) bool
- func IsMatch(str string, patterns ...string) bool
- func IsNotEmpty(str string) bool
- func IsSlice(str string) bool
- func IsUlid(str string) bool
- func IsUuid(str string) bool
- func Kebab(str string) string
- func LTrim(in string, characters ...string) string
- func LcFirst(in string) string
- func LeftFrom(str, needle string) string
- func LeftPad(s string, padStr string, overallLen int) string
- func Length(in string) int
- func Limit(str string, limit int, end ...string) string
- func Lower(in string) string
- func MD5(text string) string
- func Mask(in, character string, index int, length ...int) string
- func Match(in, pattern string) string
- func MatchAll(in, pattern string) []string
- func Maximum(a, b int) int
- func NewBuffer() *buffer
- func NewLine(in string, count ...int) string
- func PadBoth(str string, length int, pad ...string) string
- func PadLeft(str string, length int, pad ...string) string
- func PadRight(str string, length int, pad ...string) string
- func Pipe(in string, callback func(string) string) string
- func Prepend(str string, values ...string) string
- func RTrim(str string, characters ...string) string
- func Random(length int) string
- func RandomFromGamma(length int, gamma string) (string, error)
- func Remove(str string, values ...string) string
- func RemovePrefix(str string, prefix string) string
- func RemoveSuffix(str string, suffix string) string
- func Repeat(in string, times int) string
- func Replace(str, search, replace string, caseSensitive ...bool) string
- func ReplaceFirst(str, search, replace string) string
- func ReplaceLast(str, search, replace string) string
- func ReplaceMatches(str, pattern, replace string) string
- func ReplaceStart(str, search, replace string) string
- func RightFrom(str, needle string) string
- func RightPad(s string, padStr string, overallLen int) string
- func SHA256(text string) string
- func Slugify(s string, replaceWith rune) string
- func Snake(str string, delimiter ...string) string
- func Split(str, pattern string, limit ...int) []string
- func Squish(str string) string
- func Start(str, prefix string) string
- func StartsWith(str string, values ...string) bool
- func Studly(in string) string
- func Substr(str string, start int, length ...int) string
- func Swap(str string, replacements map[string]string) string
- func Tap(str string, callback func(string)) string
- func Test(str, pattern string) bool
- func Title(in string) string
- func ToBcryptHash(str string) (string, error)
- func ToBytes(s string) []byte
- func ToCamel(in string) string
- func ToPrice(price float64, currencyCode string, htmlEntity ...bool) string
- func ToPriceFromString(priceStr string, currencyCode string, htmlEntity ...bool) (string, error)
- func ToPriceFromStringOrDefault(priceStr string, currencyCode string, defaultValue string, htmlEntity ...bool) string
- func ToSnake(in string) string
- func Trim(str string, characters ...string) string
- func Truncate(str string, length int, ellipsis string) string
- func UcFirst(str string) string
- func UcSplit(s string) []string
- func Unless(str string, predicate func(string) bool, fallback func(string) string) string
- func Upper(str string) string
- func When(str string, condition bool, truthy func(string) string, ...) string
- func WhenContains(str, value string, truthy func(string) string, fallback ...func(string) string) string
- func WhenContainsAll(str string, values []string, truthy func(string) string, ...) string
- func WhenEmpty(str string, truthy func(string) string, fallback ...func(string) string) string
- func WhenEndsWith(str string, values []string, truthy func(string) string, ...) string
- func WhenExactly(str, value string, truthy func(string) string, fallback ...func(string) string) string
- func WhenIs(str string, patterns []string, truthy func(string) string, ...) string
- func WhenIsAscii(str string, truthy func(string) string, fallback ...func(string) string) string
- func WhenIsUlid(str string, truthy func(string) string, fallback ...func(string) string) string
- func WhenIsUuid(str string, truthy func(string) string, fallback ...func(string) string) string
- func WhenNotEmpty(str string, truthy func(string) string, fallback ...func(string) string) string
- func WhenNotExactly(str, value string, truthy func(string) string, fallback ...func(string) string) string
- func WhenStartsWith(str string, values []string, truthy func(string) string, ...) string
- func WordCount(str string) int
- func Words(str string, limit int, end ...string) string
- type ExcerptOption
- type StringBuilder
- func (sb *StringBuilder) Append(parts ...string) *StringBuilder
- func (sb *StringBuilder) Exactly(str string) bool
- func (sb *StringBuilder) LTrim(chars ...string) *StringBuilder
- func (sb *StringBuilder) Prepend(prefix string) *StringBuilder
- func (sb *StringBuilder) RTrim(chars ...string) *StringBuilder
- func (sb *StringBuilder) String() string
- func (sb *StringBuilder) Unless(predicate func(*StringBuilder) bool, ...) *StringBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddSlashes ¶
AddSlashes returns a string with backslashes added before characters that need to be escaped.
These characters are: single quote (') double quote (") backslash (\)
func AddSlashesCustom ¶
AddSlashesCustom returns a string with backslashes added before characters specified in the escapeChars string.
func Base32ExtendedDecode ¶
Base32ExtendedDecode encodes binary data to base32 extended (RFC 4648) encoded text.
func Base32ExtendedEncode ¶
Base32ExtendedEncode encodes binary data to base32 extended (RFC 4648) encoded text.
func Base64Decode ¶
Base64Decode decodes base64 text to binary data.
func Base64Encode ¶
Base64Encode encodes binary data to base64 encoded text.
func Basename ¶ added in v0.6.0
Basename returns the basename of the file path string, and trims the suffix based on the parameter (optional). Example: Basename("/path/to/file.txt") returns "file.txt" Example: Basename("/path/to/file.txt", ".txt") returns "file"
func BcryptHashCompare ¶
BcryptHashCompare compares the string to a bcrypt hash
func Before ¶
Before returns the substring before the first occurrence of the specified search string.
func BeforeLast ¶
BeforeLast returns the substring before the last occurrence of the specified search string.
func BetweenFirst ¶ added in v0.6.0
BetweenFirst returns the substring between the first occurrence of the start string and the first occurrence of the end string after the start. If either start or end is empty or not found, returns empty string. Example: BetweenFirst("Hello [World] and [Universe]", "[", "]") returns "World"
func CharAt ¶
CharAt returns the character at the specified index.
If the specified index is negative, it is counted from the end of the string.
Business logic: 1. Convert the string to a rune slice for proper handling of UTF-8 encoding. 2. Get the length of the rune slice. 3. Handle negative indices by converting them to positive indices (e.g. -1 -> length - 1). 4. Check if the index is out of bounds. 5. If the index is out of bounds, return an empty string. 6. Return the character at the specified index.
Example:
str.CharAt("Hello World", 0) // Returns "H"
str.CharAt("Hello World", -1) // Returns "d"
str.CharAt("Hello World", 20) // Returns ""
Parameters: - str: The string to get the character from. - index: The index of the character to get.
Returns: - The character at the specified index.
func ChopEnd ¶ added in v0.6.0
ChopEnd removes the first matching suffix found in needle or more from the end of str. If none of the provided suffixes match, the original string is returned unchanged.
func ChopStart ¶ added in v0.6.0
ChopStart removes the first matching prefix found in needle or more from the start of str. If none of the provided prefixes match, the original string is returned unchanged.
func Contains ¶ added in v0.6.0
Contains returns true if str contains any of the provided values. Empty values are ignored. If no non-empty values are provided, returns false.
func ContainsAll ¶ added in v0.6.0
ContainsAll returns true if str contains all the provided values. Empty values are treated as matches (consistent with strings.Contains behaviour).
func ContainsAnyChar ¶
ContainsAnyChar returns true if the string contains any of the characters in the provided charset
func ContainsOnly ¶
ContainsOnly returns true is the string contains only charcters from the specified charset
func CurrencySymbol ¶ added in v0.5.0
CurrencySymbol returns the symbol for the given ISO 4217 currency code. By default, returns the Unicode symbol. Pass true to get HTML entity instead. Returns the original currency code if not recognized. Example: CurrencySymbol("USD") returns "$" Example: CurrencySymbol("GBP", true) returns "£"
func Dirname ¶ added in v0.7.0
Dirname returns the directory portion of the provided path. By default it returns the parent directory; provide levels to traverse multiple parents.
func EndsWith ¶ added in v0.7.0
EndsWith returns true if str ends with any of the provided values. Empty values are ignored. If no non-empty values are provided, returns false.
func Excerpt ¶ added in v0.7.0
func Excerpt(str, phrase string, options ...ExcerptOption) string
Excerpt returns a substring surrounding the first occurrence of phrase. The radius controls how many runes are kept on each side of the phrase, and omission specifies the string to append when content is truncated. If phrase is not found, the original string is returned unchanged.
func Explode ¶ added in v0.7.0
Explode splits the input string using the provided delimiter. An optional limit mirrors PHP's explode semantics:
>0 keeps at most limit elements, combining the remainder into the last element. <0 omits the last |limit| elements. 0 is treated as if no limit was provided.
func Finish ¶ added in v0.7.0
Finish appends the suffix to str, ensuring it appears only once at the end.
func Headline ¶ added in v0.7.0
Headline converts a string into headline case. When the input already contains whitespace, it is simply title-cased. Otherwise, the string is normalized, converted to studly case, split on uppercase boundaries, and the resulting words are joined with spaces.
func IntToBase32 ¶
func IntToBase36 ¶
func IsNotEmpty ¶
IsNotEmpty returns true if the string is not empty.
func LTrim ¶ added in v0.8.0
LTrim removes leading characters from the input string. When no character set is provided, leading Unicode whitespace is trimmed.
func Limit ¶ added in v0.16.0
Limit truncates the string to the given length, appending the provided ending (or "..." by default) when truncation occurs. Length is measured in runes.
func MD5 ¶
MD5 converts a string to an MD5 hash.
WARNING: MD5 is cryptographically broken and must not be used for security- sensitive purposes such as password hashing or integrity checks. This helper is retained for backward compatibility only; prefer SHA-256 via SHA256 (or stronger) for general hashing needs and BCrypt for passwords.
func Mask ¶ added in v0.8.0
Mask replaces a portion of the string with a repeated masking character. The index marks the starting rune offset, with negative values counting backwards from the end. If length is omitted the remainder of the string is masked. When the masking character is empty or the target segment is empty, the original string is returned unchanged.
func Match ¶ added in v0.8.0
Match returns the first substring that matches the provided regular expression. If the pattern is empty, the original string is returned unchanged.
func MatchAll ¶ added in v0.8.0
MatchAll returns all substrings that match the provided regular expression. If the pattern is empty, the entire string is returned as the single result.
func NewLine ¶ added in v0.8.0
NewLine appends new line characters to the input string. When no count is provided, a single newline is appended. Otherwise, count[0] specifies how many newline characters to add.
func PadBoth ¶ added in v0.8.0
PadBoth pads the string on both sides to reach the desired length. Padding is evenly split between the left and right; when an odd number of characters is required, the extra character is added to the right side. The optional pad argument specifies the padding characters; it defaults to a single space when omitted or empty.
func PadLeft ¶ added in v0.8.0
PadLeft pads the string on the left to reach the specified length. When the pad argument is omitted or empty, spaces are used.
func PadRight ¶ added in v0.8.0
PadRight pads the string on the right to reach the specified length. When the pad argument is omitted or empty, spaces are used.
func Pipe ¶ added in v0.8.0
Pipe passes the string through the provided callback and returns the result. If callback is nil, the original string is returned unchanged.
func RTrim ¶ added in v0.13.0
RTrim removes trailing characters from the input string. When no characters are provided, Unicode whitespace is trimmed.
func Random ¶
Random generates an alphanumeric string of the specified length using crypto/rand with rejection sampling to keep the character distribution unbiased.
Business Logic:
- Directly using raw crypto/rand bytes (e.g. by base64-encoding and truncating) skews character frequency, so we discard out-of-range samples before mapping.
- Alphanumeric output (letters and digits only) keeps the resulting tokens URL, filename, and copy/paste friendly.
func RandomFromGamma ¶
RandomFromGamma generates a random string of specified length using the characters provided in gamma. It returns an error when length is not positive, gamma is empty, or when secure randomness cannot be generated.
func Remove ¶ added in v0.10.0
Remove deletes all occurrences of the provided values from the input string.
func RemovePrefix ¶
func RemoveSuffix ¶
func Replace ¶ added in v0.11.0
Replace substitutes occurrences of search with replace. Case sensitivity can be controlled via the optional caseSensitive flag (defaults to true).
func ReplaceFirst ¶ added in v0.11.0
ReplaceFirst replaces the first occurrence of search with replace. If search is empty, the original string is returned unchanged.
func ReplaceLast ¶ added in v0.12.0
ReplaceLast replaces the last occurrence of search with replace. If search is empty or not found, the original string is returned unchanged.
func ReplaceMatches ¶ added in v0.12.0
ReplaceMatches replaces all substrings matching the pattern with the provided replacement string. If the pattern is empty, the original string is returned unchanged.
func ReplaceStart ¶ added in v0.12.0
ReplaceStart replaces the beginning of the string if it starts with search. If search is empty or not present at the start, the original string is returned.
func SHA256 ¶ added in v0.17.0
SHA256 returns the SHA-256 hash of the provided text encoded as a hex string.
func Slugify ¶
StrSlugify replaces each run of characters which are not ASCII letters or numbers with the Replacement character, except for leading or trailing runs. Letters will be stripped of diacritical marks and lowercased. Letter or number codepoints that do not have combining marks or a lower-cased variant will be passed through unaltered.
func Snake ¶ added in v0.13.0
Snake converts the string to snake case using the provided delimiter. The delimiter defaults to underscore when not specified or empty.
func Split ¶ added in v0.13.0
Split divides the string using the provided regex pattern. The optional limit matches regexp.Split behaviour: default -1 keeps all substrings.
func Squish ¶ added in v0.13.0
Squish collapses consecutive whitespace into a single space and trims leading/trailing whitespace.
func Start ¶ added in v0.14.0
Start ensures the string begins with the provided prefix without duplicating it. If the prefix is empty, the original string is returned.
func StartsWith ¶ added in v0.14.0
StartsWith returns true if the string starts with any of the provided values. When no values are supplied, it returns false.
func Studly ¶ added in v0.7.0
Studly converts a string to StudlyCase (also known as UpperCamelCase).
func Substr ¶
Substr returns a substring of a given string, starting at the specified index and with a specified length. It handles UTF-8 encoded strings.
func Swap ¶ added in v0.14.0
Swap replaces occurrences of keys in the replacements map with their values. If the map is empty or contains an empty key, the original string is returned.
func Tap ¶ added in v0.14.0
Tap passes the string to the provided callback and returns the original string unchanged. A nil callback is safely ignored.
func Test ¶ added in v0.16.0
Test returns true if the string matches the given regular expression pattern. Empty patterns always return false.
func Title ¶ added in v0.7.0
Title returns the input string converted to title case using the default language-independent rules.
func ToBcryptHash ¶
ToBcryptHash converts the string to bcrypt hash
func ToPrice ¶ added in v0.5.0
ToPrice converts a float64 price to a formatted string with the given ISO 4217 currency code. The price is formatted to 2 decimal places. By default, returns Unicode symbol. Pass true to get HTML entity instead. Example: ToPrice(19.99, "USD") returns "$19.99" Example: ToPrice(19.99, "GBP", true) returns "£19.99"
func ToPriceFromString ¶ added in v0.5.0
ToPriceFromString converts a string price to a formatted price string with the given ISO 4217 currency code. Returns an error if the string cannot be parsed as a float. The price is formatted to 2 decimal places. By default, returns Unicode symbol. Pass true to get HTML entity instead. Example: ToPriceFromString("19.99", "USD") returns "$19.99", nil Example: ToPriceFromString("19.99", "GBP", true) returns "£19.99", nil
func ToPriceFromStringOrDefault ¶ added in v0.5.0
func ToPriceFromStringOrDefault(priceStr string, currencyCode string, defaultValue string, htmlEntity ...bool) string
ToPriceFromStringOrDefault converts a string price to a formatted price string with the given ISO 4217 currency code. Returns the defaultValue if the string cannot be parsed as a float. The price is formatted to 2 decimal places. By default, returns Unicode symbol. Pass true to get HTML entity instead. Example: ToPriceFromStringOrDefault("19.99", "USD", "n/a") returns "$19.99" Example: ToPriceFromStringOrDefault("invalid", "USD", "n/a") returns "n/a" Example: ToPriceFromStringOrDefault("19.99", "GBP", "n/a", true) returns "£19.99"
func ToSnake ¶
ToSnake convert the given string to snake case following the Golang format: acronyms are converted to lower-case and preceded by an underscore.
func Trim ¶ added in v0.15.0
Trim removes the provided characters from both ends of the string. When no characters are provided, Unicode spaces are removed.
func Unless ¶ added in v0.15.0
Unless executes the fallback when the predicate returns false, otherwise it returns the original string unchanged.
func When ¶ added in v0.16.0
func When(str string, condition bool, truthy func(string) string, fallback ...func(string) string) string
When executes the truthy callback when condition is true, otherwise executes the fallback if provided. It returns the resulting string.
func WhenContains ¶ added in v0.16.0
func WhenContains(str, value string, truthy func(string) string, fallback ...func(string) string) string
WhenContains executes the truthy callback when the string contains value. Otherwise it executes the fallback if provided.
func WhenContainsAll ¶ added in v0.16.0
func WhenContainsAll(str string, values []string, truthy func(string) string, fallback ...func(string) string) string
WhenContainsAll executes the truthy callback when the string contains all provided values. Otherwise it executes the fallback if present.
func WhenEmpty ¶ added in v0.16.0
WhenEmpty executes the truthy callback when the string is empty; otherwise it executes the fallback if supplied.
func WhenEndsWith ¶ added in v0.16.0
func WhenEndsWith(str string, values []string, truthy func(string) string, fallback ...func(string) string) string
WhenEndsWith executes the truthy callback when the string ends with any of the provided values; otherwise it executes the fallback if present.
func WhenExactly ¶ added in v0.16.0
func WhenExactly(str, value string, truthy func(string) string, fallback ...func(string) string) string
WhenExactly executes the truthy callback when the string exactly matches the provided value; otherwise it executes the fallback if present.
func WhenIs ¶ added in v0.16.0
func WhenIs(str string, patterns []string, truthy func(string) string, fallback ...func(string) string) string
WhenIs executes the truthy callback when the string matches any of the provided glob patterns; otherwise it executes the fallback if present.
func WhenIsAscii ¶ added in v0.16.0
WhenIsAscii executes the truthy callback when the string is ASCII-only; otherwise it executes the fallback if supplied.
func WhenIsUlid ¶ added in v0.16.0
WhenIsUlid executes the truthy callback when the string is a valid ULID; otherwise it executes the fallback if present.
func WhenIsUuid ¶ added in v0.16.0
WhenIsUuid executes the truthy callback when the string is a valid UUID; otherwise it executes the fallback if present.
func WhenNotEmpty ¶ added in v0.16.0
WhenNotEmpty executes the truthy callback when the string is not empty; otherwise it executes the fallback if provided.
func WhenNotExactly ¶ added in v0.16.0
func WhenNotExactly(str, value string, truthy func(string) string, fallback ...func(string) string) string
WhenNotExactly executes the truthy callback when the string does not exactly match the provided value; otherwise it executes the fallback if present.
func WhenStartsWith ¶ added in v0.16.0
func WhenStartsWith(str string, values []string, truthy func(string) string, fallback ...func(string) string) string
WhenStartsWith executes the truthy callback when the string starts with any of the provided values; otherwise it executes the fallback if present.
Types ¶
type ExcerptOption ¶ added in v0.7.0
ExcerptOption configures the behaviour of Excerpt.
type StringBuilder ¶ added in v0.7.0
type StringBuilder struct {
// contains filtered or unexported fields
}
func Of ¶ added in v0.7.0
func Of(value string) *StringBuilder
func (*StringBuilder) Append ¶ added in v0.7.0
func (sb *StringBuilder) Append(parts ...string) *StringBuilder
func (*StringBuilder) Exactly ¶ added in v0.7.0
func (sb *StringBuilder) Exactly(str string) bool
func (*StringBuilder) LTrim ¶ added in v0.7.0
func (sb *StringBuilder) LTrim(chars ...string) *StringBuilder
func (*StringBuilder) Prepend ¶ added in v0.7.0
func (sb *StringBuilder) Prepend(prefix string) *StringBuilder
func (*StringBuilder) RTrim ¶ added in v0.7.0
func (sb *StringBuilder) RTrim(chars ...string) *StringBuilder
func (*StringBuilder) String ¶ added in v0.7.0
func (sb *StringBuilder) String() string
func (*StringBuilder) Unless ¶ added in v0.7.0
func (sb *StringBuilder) Unless(predicate func(*StringBuilder) bool, fallback func(*StringBuilder) *StringBuilder) *StringBuilder
Source Files
¶
- add_slashes.go
- after.go
- after_last.go
- append.go
- base32_extended_decode.go
- base32_extended_encode.go
- base64_decode.go
- base64_encode.go
- basename.go
- bcrypt_hash_compare.go
- before.go
- before_last.go
- between.go
- between_first.go
- buffer.go
- builders.go
- char_at.go
- chop_end.go
- chop_start.go
- contains.go
- contains_all.go
- contains_any_char.go
- contains_only.go
- currency_symbol.go
- dirname.go
- ends_with.go
- exactly.go
- excerpt.go
- explode.go
- finish.go
- functions.go
- headline.go
- int_to_base32.go
- int_to_base36.go
- is.go
- is_match.go
- isascii.go
- isempty.go
- ismap.go
- isnotempty.go
- isslice.go
- isulid.go
- isuuid.go
- kebab.go
- lc_first.go
- left_from.go
- left_pad.go
- length.go
- limit.go
- lower.go
- ltrim.go
- mask.go
- match.go
- match_all.go
- maximum.go
- md5.go
- newline.go
- pad_both.go
- pad_left.go
- pad_right.go
- pipe.go
- prepend.go
- random.go
- random_from_gamma.go
- remove.go
- remove_prefix.go
- remove_suffix.go
- repeat.go
- replace.go
- replace_first.go
- replace_last.go
- replace_matches.go
- replace_start.go
- right_from.go
- right_pad.go
- rtrim.go
- sha256.go
- slugify.go
- snake.go
- split.go
- squish.go
- start.go
- starts_with.go
- studly.go
- substr.go
- swap.go
- tap.go
- test.go
- title.go
- to_bcrypt_hash.go
- to_bytes.go
- to_camel.go
- to_price.go
- to_price_from_string.go
- to_snake.go
- trim.go
- truncate.go
- uc_first.go
- uc_split.go
- unless.go
- upper.go
- when.go
- when_contains.go
- when_contains_all.go
- when_empty.go
- when_ends_with.go
- when_exactly.go
- when_is.go
- when_is_ascii.go
- when_is_ulid.go
- when_is_uuid.go
- when_not_empty.go
- when_not_exactly.go
- when_starts_with.go
- word_count.go
- words.go