strings

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2023 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dedent

func Dedent(s string) string

Dedent takes a string and returns the same string with all leading whitespace characters removed, with a maximum of the smallest count of those characters. It is very similar to Python's textwrap.dedent(). Dedent also removes any newline-only lines.

For example, the following string (which I need to write this way because of godoc formatting):

\tabc\n\t\teasy as\n\t\t\tone two three

would be returned as:

abc\t\neasy as\n\t\tone two three

which is dedented by one tab character (the smallest amount discovered).

Note that this implementation depends on the line separator being LF (`\n`).

Example

Dedent is most helpful when writing embedded multiline strings, that typically you would need to align fully to the first column of your file. Using Dedent allows for indenting for easier readability without the ugly break in visual scans.

theUglyWayYouUsuallyHaveToDoIt := `---
some: yaml_frontmatter
---
key1:
	key1a: value
	key2a:
		key2aa: value
key2: value`

indentedString := `
		---
		some: yaml_frontmatter
		---
		key1:
			key1a: value
			key2a:
				key2aa: value
		key2: value
	`
dedentedString := Dedent(indentedString)

fmt.Println(dedentedString == theUglyWayYouUsuallyHaveToDoIt)
Output:

true

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL