Documentation
¶
Overview ¶
String based data structures and string manipulation functions.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func 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 ¶
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 StripWhitespace ¶
Given a string, will strip all whitespace from it 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
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
Compares two string elements in the StringHeap.
Uses strings.Compare internally.
func (*StringHeap) Push ¶
func (spq *StringHeap) Push(x interface{})
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)
Swaps the two elements indicated via the given indices.