Skip to content
Go library for truncating strings
Go
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
example_test.go
go.mod
truncate.go
truncate_test.go

README.md

truncate

Build Status 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
}
You can’t perform that action at this time.