Documentation
¶
Overview ¶
Package strings provides string utilities to the goradd framework.
Index ¶
- Constants
- func AtoI[T constraints.Integer](s string) T
- func Between(s, left, right string) string
- func Camel(s string) string
- func CamelToKebab(camelCase string) string
- func CamelToSnake(camelCase string) string
- func Connect(sep string, items ...string) string
- func ContainsAnyStrings(haystack string, needles ...string) bool
- func CryptoString(source string, n int) string
- func Decap(s string) string
- func EndsWith(s string, ending string) bool
- func EqualCaseInsensitive(s1, s2 string) bool
- func ExtractNumbers(in string) string
- func HasCharType(s string, wantUpper, wantLower, wantDigit, wantPunc, wantSymbol bool) bool
- func HasNull(s string) bool
- func HasOnlyLetters(s string) bool
- func If(cond bool, trueVal, falseVal string) string
- func Indent(s string) string
- func IsASCII(s string) bool
- func IsFloat(s string) bool
- func IsInt(s string) bool
- func IsSnake(s string) bool
- func IsUTF8(s string) bool
- func IsUTF8Bytes(b []byte) bool
- func IsWhitespace(s string) bool
- func KebabToCamel(s string) string
- func PasswordString(n int) string
- func Plural(s string) string
- func RandomString(source string, n int) string
- func ReplaceOldNew(s string, searchList ...string) string
- func ReplaceStrings(s string, searchList []string, replaceList []string) string
- func SnakeToCamel(s string) string
- func SnakeToKebab(s string) string
- func StartsWith(s string, beginning string) bool
- func StripNewlines(s string) string
- func StripNulls(s string) string
- func Title(s string) string
- func TrimRightSpace(s string) string
- func TrimShiftLines(s string) string
Examples ¶
Constants ¶
const AlphaLower = "abcdefghijklmnopqrstuvwxyz"
const AlphaNumeric = AlphaLower + AlphaUpper + Numbers
const AlphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const Numbers = "0123456789"
const PasswordBytes = PasswordLower + PasswordUpper + PasswordNum + PasswordSym
const PasswordLower = "abcdefghijkmnopqrstuvwxyz"
const PasswordNum = "23456789"
const PasswordSym = "!@#%?+=_"
const PasswordUpper = "ABCDEFGHJKLMNPQRSTUVWXYZ"
const Token68 = AlphaNumeric + "-._~+/"
Variables ¶
This section is empty.
Functions ¶
func AtoI ¶
func AtoI[T constraints.Integer](s string) T
AtoI is a convenience script for converting a string to various types of signed integers. An invalid input will return zero, including if the input overflows the max size of the integer type.
Example ¶
i := AtoI[uint8]("23")
fmt.Println(i)
Output: 23
func Between ¶ added in v0.3.0
Between returns the string between the left and right values in s. If left or right are not in s, all of s is returned.
func Camel ¶ added in v0.3.0
Camel converts a string separated by spaces, hyphens and other breaks to CamelCase, also known as PascalCase. None alpha-numerics are removed.
func CamelToKebab ¶
CamelToKebab converts capitalize from CamelCase to kebab-case. If it encounters a character that is not legitimate camel case, it ignores it (like numbers, spaces, etc.). Runs of upper case letters are treated as one word.
Example ¶
a := CamelToKebab("AbcDef")
fmt.Println(a)
b := CamelToKebab("AbcDEFghi")
fmt.Println(b)
Output: abc-def abc-de-fghi
func CamelToSnake ¶
CamelToSnake converts camelCase from CamelCase to snake_case. If it encounters a character that is not legitimate camel case, it ignores it (like numbers, spaces, etc.). Runs of upper case letters are treated as one word. A run of upper case, followed by lower case letters will be treated as if the final character in the upper case run belongs with the lower case letters.
Example ¶
a := CamelToSnake("AbcDef")
fmt.Println(a)
b := CamelToSnake("AbcDEFghi")
fmt.Println(b)
Output: abc_def abc_de_fghi
func Connect ¶
Connect joins strings together with the separator sep. Only strings that are not empty strings are joined.
Example ¶
a := Connect("+", "this", "", "that")
fmt.Println(a)
Output: this+that
func ContainsAnyStrings ¶
ContainsAnyStrings returns true if the haystack contains any of the needles
func CryptoString ¶
CryptoString returns a cryptographically secure random string from the given source. Use AlphaNumeric, AlphaUpper, AlphaLower, or Numbers as shortcuts for source.
func Decap ¶ added in v0.2.0
Decap returns a new string with the first character in the string set to its lower case equivalent, and subsequent characters that are capitalized also set to lower case until it encounters a lower case letter.
func EqualCaseInsensitive ¶ added in v0.3.0
EqualCaseInsensitive is a synonym for the strings package EqualFold which provides unicode compliant case-insensitive comparison.
func ExtractNumbers ¶
ExtractNumbers returns a string with the digits contained in the given string.
Example ¶
a := ExtractNumbers("a1b2 c3")
fmt.Println(a)
Output: 123
func HasCharType ¶
HasCharType returns true if the given string has at least one of the selected char types.
func HasNull ¶
HasNull returns true if the given string has a null character in it. Null characters are highly unusual in a string, and can indicate that an attempt is being made to plant hidden data into storage.
func HasOnlyLetters ¶
HasOnlyLetters will return false if any of the characters in the string do not pass the unicode.IsLetter test.
func If ¶
If is like the ternary operator ?. It returns the first string on true, and the second on false.
func IsFloat ¶
IsFloat returns true if the given string is a floating point number. Allows the string to start with a + or -.
func IsInt ¶
IsInt returns true if the given string is an integer. Allows the string to start with a + or -.
func IsSnake ¶ added in v0.3.0
IsSnake returns true if the string only contains lower case letters, numbers and underscores with no spaces.
func IsUTF8Bytes ¶
IsUTF8Bytes returns true if the given byte array only contains valid UTF-8 characters
func IsWhitespace ¶ added in v0.3.0
IsWhitespace returns true if the given string only contains whitespace characters.
func KebabToCamel ¶
KebabToCamel convert string s from kebab-case to CamelCase. The initial case of s does not affect the output.
Example ¶
a := KebabToCamel("abc-def")
fmt.Println(a)
Output: AbcDef
func PasswordString ¶
PasswordString generates a pseudo random password with the given length using characters that are common in passwords. We leave out letters that are easily visually confused.
Specific letters excluded are lowercase l, upper case I and the number 1, upper case O and the number 0. Also, only easily identifiable and describable symbols are used.
It tries to protect against accidentally creating an easily guessed value by making sure the password has at least one lower-case letter, one upper-case letter, one number, and one symbol. n must be at least 4, or an empty string will be returned.
func Plural ¶ added in v0.3.0
Plural returns the plural version of the given string. This relies on a third party library, which may or may not be accurate. The goal is to handle the most common cases.
func RandomString ¶
RandomString generates a pseudo random string of the given length using the given characters. The distribution is not perfect, but works for general purposes.
func ReplaceOldNew ¶ added in v0.3.0
ReplaceOldNew replaces every string in oldNew with the string following it, such that each pair of strings forms an old/new pair.
func ReplaceStrings ¶
ReplaceStrings replaces every string in the searchList with the matching string in the replaceList. Will panic if searchList len does not match replaceList len, or anything else goes wrong in the replacement.
func SnakeToCamel ¶ added in v0.3.0
SnakeToCamel converts s from snake_case to CamelCase.
func SnakeToKebab ¶
SnakeToKebab converts s from snake_case to kebab-case. Only the underscores are impacted, the case of the rest of s is passed through unchanged.
Example ¶
a := SnakeToKebab("abc_def")
fmt.Println(a)
Output: abc-def
func StartsWith ¶
StartsWith returns true if the string begins with the beginning string.
func StripNewlines ¶
StripNewlines removes all newline characters from a string.
func StripNulls ¶
StripNulls removes null characters from a string. Null characters are highly unusual in a string and may indicate an attempt to plant hidden information.
func Title ¶
Title is a more advanced titling operation. It will convert underscores to spaces, and add spaces to CamelCase words.
Example ¶
a := Title("do_i_seeYou")
fmt.Println(a)
Output: Do I See You
func TrimRightSpace ¶ added in v0.3.0
TrimRightSpace returns a string with all its trailing space characters removed.
func TrimShiftLines ¶ added in v0.3.0
TrimShiftLines will trim each line in s (separated by newlines) as follows:
If the line is only whitespace, it will become an empty line.
Whitespace at the end of each line will be removed.
Each line that has content will be examined for whitespace in front of the content, and the smallest length of white space found in all the lines will then be removed from the front of every line, provided. that whitespace is found at the front of every line. Note that tabs will not be converted to spaces. The whitespace characters must match. The effect will be to shift all the lines to the left so that the line closest to the left will now be flush with the left, and all other lines will be shifted left the same amount.
Types ¶
This section is empty.