Documentation ¶
Overview ¶
Package wordwrap provides a word-wrapping function that returns the wrapped lines as a slice of strings.
Example ¶
Words are wrapped at whitespace and at hyphens. Wrapping whitespace is trimmed, hyphens are kept. If a word is too long to fit in the limit, it will be broken at the limit.
lines := wordwrap.WordWrap("this test-string has been successfully wrapped successfully", 10) for _, line := range lines { fmt.Println(line) }
Output: this test- string has been successful ly wrapped successful ly
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WordWrap ¶
WordWrap wraps a string, where each "line" of the wrapped string is an item of a slice of strings. A limit of zero or less is treated as an infinite limit.
If a word is longer than the limit would be allowed even after breaking on word separators, then the word itself will be wrapped.
Whitespace such as spaces and tabs are considered word separators that can excluded from the output. Hyphens are also considered word separators, but will be included at the end of a line.
Example (Hyphen) ¶
Words are wrapped at hyphens, and the hyphens are kept at the ends of lines.
lines := wordwrap.WordWrap("hyphenated-words", 15) for _, line := range lines { fmt.Println(line) }
Output: hyphenated- words
Example (Long) ¶
Words that are too long for the limit will be broken.
lines := wordwrap.WordWrap("longwordsarewrapped", 10) for _, line := range lines { fmt.Println(line) }
Output: longwordsa rewrapped
Example (Space) ¶
Words are wrapped at spaces.
lines := wordwrap.WordWrap("we wrap at spaces", 9) for _, line := range lines { fmt.Println(line) }
Output: we wrap at spaces
Types ¶
This section is empty.