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 interface{}) string
- type StringHeap
Examples ¶
Constants ¶
const ( AlphaLower = "abcdefghijklmnopqrstuvwxyz" AlphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" Alpha = AlphaLower + AlphaUpper Numeric = "1234567890" AlphaNumeric = Alpha + Numeric )
Variables ¶
This section is empty.
Functions ¶
func IsAlpha ¶ added in v1.2.6
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 ¶ added in v1.2.6
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 ¶ added in v1.2.6
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 ¶ added in v1.2.7
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 ¶ added in v1.2.7
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 ¶ added in v1.2.1
func TypeName(i interface{}) string
The TypeName of the given interface{}.
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 ¶
type StringHeap ¶
type StringHeap []string
StringHeap is a priority queue which sorts strings lexicographically.
Example ¶
How to create and use a StringHeap.
Just uses the usual standard heap package functions.
// Create a StringHeap. stringHeap := make(StringHeap, 0) heap.Init(&stringHeap) // Push some strings. Make sure to use heap.Push rather than stringHeap.Push. heap.Push(&stringHeap, "Crisps") heap.Push(&stringHeap, "Egg") heap.Push(&stringHeap, "Bananas") heap.Push(&stringHeap, "Doughnut") heap.Push(&stringHeap, "Apple") heap.Push(&stringHeap, "Fried chicken") heap.Push(&stringHeap, "Orange") heap.Push(&stringHeap, "Grapefruit") // We can get the length using stringHeap.Len. fmt.Println("Length before:", stringHeap.Len()) // Pop them off. for stringHeap.Len() > 0 { fmt.Println(heap.Pop(&stringHeap).(string)) } fmt.Println("Length after:", stringHeap.Len())
Output: Length before: 8 Apple Bananas Crisps Doughnut Egg Fried chicken Grapefruit Orange Length after: 0
func (StringHeap) Less ¶
func (spq StringHeap) Less(i, j int) bool
Less compares two string elements in the StringHeap.
Uses strings.Compare internally.
func (*StringHeap) Push ¶
func (spq *StringHeap) Push(x interface{})
Push pushes the given interface{} to the heap.
Will panic if given interface{} is not a string.
func (StringHeap) Swap ¶
func (spq StringHeap) Swap(i, j int)
Swap swaps the two elements indicated via the given indices.