Categorygithub.com/aquilax/truncate
repositorypackage
1.0.1
Repository: https://github.com/aquilax/truncate.git
Documentation: pkg.go.dev

# README

truncate

GoDoc

Go library for truncating strings

Sample usage:

package truncate_test

import (
	"fmt"
	"unicode/utf8"

	"github.com/aquilax/truncate"
)

func ExampleTruncate() {
	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
}

func ExampleTruncate_second() {
	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
}

func ExampleTruncate_third() {
	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 ExampleTruncator() {
	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
}