Documentation
¶
Overview ¶
Package truncate provides set of strategies to truncate strings
Index ¶
Examples ¶
Constants ¶
View Source
const DEFAULT_OMISSION = "…"
Variables ¶
This section is empty.
Functions ¶
func Truncate ¶
func Truncate(str string, length int, omission string, pos TruncatePosition) string
Truncate truncates string according the parameters
Example ¶
package main import ( "fmt" "unicode/utf8" "github.com/aquilax/truncate" ) func main() { text := "This is a long text" truncated := truncate.Truncate(text, 17, "...", truncate.PositionEnd) fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated)) }
Output: This is a long... : 17 characters
Example (Second) ¶
package main import ( "fmt" "unicode/utf8" "github.com/aquilax/truncate" ) func main() { text := "This is a long text" truncated := truncate.Truncate(text, 15, "...", truncate.PositionStart) fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated)) }
Output: ... a long text : 15 characters
Example (Third) ¶
package main import ( "fmt" "unicode/utf8" "github.com/aquilax/truncate" ) func main() { text := "This is a long text" truncated := truncate.Truncate(text, 5, "zzz", truncate.PositionMiddle) fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated)) }
Output: Tzzzt : 5 characters
func Truncator ¶
Truncator cuts a string to length using the truncation strategy
Example ¶
package main import ( "fmt" "unicode/utf8" "github.com/aquilax/truncate" ) func main() { text := "This is a long text" truncated := truncate.Truncator(text, 9, truncate.CutStrategy{}) fmt.Printf("%s : %d characters", truncated, utf8.RuneCountInString(truncated)) }
Output: This is a : 9 characters
Types ¶
type CutEllipsisLeadingStrategy ¶
type CutEllipsisLeadingStrategy struct{}
CutEllipsisLeadingStrategy simply truncates the string from the start the desired length and adds ellipsis at the front
type CutEllipsisStrategy ¶
type CutEllipsisStrategy struct{}
CutEllipsisStrategy simply truncates the string to the desired length and adds ellipsis at the end
type CutStrategy ¶
type CutStrategy struct{}
CutStrategy simply truncates the string to the desired length
type EllipsisMiddleStrategy ¶
type EllipsisMiddleStrategy struct{}
EllipsisMiddleStrategy truncates the string to the desired length and adds ellipsis in the middle
type TruncatePosition ¶
type TruncatePosition int
const ( PositionStart TruncatePosition = iota PositionMiddle PositionEnd )
Click to show internal directories.
Click to hide internal directories.