Documentation ¶
Overview ¶
Package strings contains based data structures and string manipulation functions as well as some constants.
Index ¶
- Constants
- func IsAlpha(s string) bool
- func IsAlphaNumeric(s string) bool
- func IsNumeric(s string) bool
- func JoinCamelcase(s, sep string) string
- func ReplaceCharIndex(old string, indices []int, new ...string) string
- func ReplaceCharIndexRange(old string, indices [][]int, new ...string) string
- func SplitCamelcase(s string) []string
- func StripWhitespace(str string) string
- func TypeName(i any) string
Examples ¶
Constants ¶
const ( AlphaLower = "abcdefghijklmnopqrstuvwxyz" AlphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" Alpha = AlphaLower + AlphaUpper Numeric = "1234567890" AlphaNumeric = Alpha + Numeric )
Variables ¶
This section is empty.
Functions ¶
func IsAlpha ¶
IsAlpha checks whether the given string contains only letters.
Example ¶
Checks whether "abc" and "abc?" contain letters only.
fmt.Println(IsAlpha("abc")) fmt.Println(IsAlpha("abc?"))
Output: true false
func IsAlphaNumeric ¶
IsAlphaNumeric checks whether the given string contains only letters and numbers.
Example ¶
Checks whether "abc123" and "abc123?!" are alphanumeric strings.
fmt.Println(IsAlphaNumeric("abc123")) fmt.Println(IsAlphaNumeric("abc123?!"))
Output: true false
func IsNumeric ¶
IsNumeric checks whether the given string contains only numbers.
Example ¶
Checks whether "123" and "123abc" contain numbers only.
fmt.Println(IsNumeric("123")) fmt.Println(IsNumeric("123abc"))
Output: true false
func JoinCamelcase ¶
JoinCamelcase replaces the hump boundaries of the given camelcase-d string with the given separator.
For example joining the following string...
"HelloWorld"
... With ", " , produces:
"Hello, World"
Example ¶
Joins each hump of the camelcase string with the given separator.
fmt.Println(JoinCamelcase("HelloWorld", ", "))
Output: Hello, World
func ReplaceCharIndex ¶
ReplaceCharIndex will replace all characters at the given indices with the new strings. Returns a new string.
Indices are all the character indices with which to replace with the new string. Each occurrence will be replaced by the string new[occCount % len(new)]. Where occCount is the current count of the indices that have been replaced.
The indices slice can contain duplicates and doesn't need to be sorted.
Example ¶
Will replace the given character indices within the given string with the given new strings in the order given.
old := "Hello world! Hope you are having a good day today." fmt.Println(ReplaceCharIndex(old, []int{1, 2, 7, 9, 10, 14, 20, 41}, "Fizz", "Buzz", "Bing"))
Output: HFizzBuzzlo wBingrFizzBuzz! HBingpe yoFizz are having a good dBuzzy today.
func ReplaceCharIndexRange ¶
ReplaceCharIndexRange is similar to ReplaceCharIndex but takes multiple index ranges in the form of [start, end].
The length of new strings must be less than or equal to the length of the indices slice. The length of indices must also be greater than 0. If any of these conditions are not met the old string shall be returned.
The indices slice can contain duplicates and doesn't need to be sorted. It's worth bearing in mind that removing the duplicates from the indices slice is O(n^2).
Example ¶
Will replace "world" with "me", "you" with "I" and "are" with "am".
old := "Hello world! Hope you are having a good day today." fmt.Println(ReplaceCharIndexRange(old, [][]int{{6, 11}, {18, 21}, {22, 25}}, "me", "I", "am"))
Output: Hello me! Hope I am having a good day today.
func SplitCamelcase ¶
SplitCamelcase splits a string containing camelcase at each hump.
For example the following string:
"HelloWorld"
Would produce:
{"Hello", "World"}
Example ¶
Splits a string which is in camelcase at each hump.
fmt.Println(SplitCamelcase("HelloWorld"))
Output: [Hello World]
func StripWhitespace ¶
StripWhitespace will strip all whitespace from a given string and return a new string without any whitespace.
func TypeName ¶
The TypeName of the given any.
If i is nil, "<nil>" will be returned.
Example ¶
Get the type name of a string then nil.
val := "hello world" fmt.Printf("The type of \"%v\" is \"%s\"\n", val, TypeName(val)) fmt.Printf("Type type of \"%v\" is \"%s\"\n", nil, TypeName(nil))
Output: The type of "hello world" is "string" Type type of "<nil>" is "<nil>"
Types ¶
This section is empty.