Categorygithub.com/ozgio/strutil
modulepackage
0.4.0
Repository: https://github.com/ozgio/strutil.git
Documentation: pkg.go.dev

# README

String Utilities for Go

Tests GoReportCard GoDocs

"strutil" provides string functions for go applications.

For documentation with examples see GoDoc

Functions

Align (Docs)

Aligns the text to the specified side

strutil.Align("lorem ipsum", strutil.Right, 20) //->"        lorem ipsum" 

AlignCenter (Docs)

Aligns the text to the center

strutil.AlignCenter("lorem\nipsum", 10) //->"  lorem   \n  ipsum   " 

AlignLeft (Docs)

Aligns the text to the left

strutil.AlignLeft("  lorem   \n  ipsum   ") //->"lorem   \nipsum   " 

AlignRight (Docs)

Aligns the text to the right

strutil.AlignRight("lorem\nipsum", 10) //-> "     lorem\n     ipsum" 

CountWords (Docs)

Counts the words

strutil.CountWords("Lorem ipsum, dolor sit amet") //-> "5"

DrawBox (Docs)

Draws a frame around the string with default character set

strutil.DrawBox("Hello World", 20, strutil.Center)
//┌──────────────────┐
//│   Hello World    │
//└──────────────────┘

DrawCustomBox (Docs)

Draws a frame around the string with custom character set

strutil.DrawCustomBox("Hello World", 20, strutil.Center, strutil.SimpleBox9Slice(), "\n")
//┌──────────────────┐
//│   Hello World    │
//└──────────────────┘

ExpandTabs (Docs)

Converts tabs to the spaces

strutil.ExpandTabs("\tlorem\n\tipsum\n", 2) //-> "  lorem\n  ipsum\n"

Indent (Docs)

Indents every line

strutil.Indent("lorem\nipsum", "> ") //-> "> lorem\n> ipsum"

IsASCII (Docs)

Checks if all the characters in string are in standard ASCII table

strutil.IsASCII("lorem\nipsum") //-> true

Len (Docs)

Alias of utf8.RuneCountInString which returns the number of runes in string

strutil.Len("böt") //-> "3"

MapLines (Docs)

Runs function fn on every line of the string

strutil.MapLines("   lorem      \n    ipsum      ", strings.TrimSpace) //-> "lorem\nipsum"

OSNewLine (Docs)

OSNewLine returns operating systems default new line character

strutil.OSNewLine() //-> "\n"

Pad (Docs)

Left and right pads the string

strutil.Pad("lorem", 11, "->", "<-") //-> "->->lorem<-<-"

PadLeft (Docs)

Left pads the string

strutil.PadLeft("lorem", 9, "->") //-> "->->lorem"

PadRight (Docs)

Right pads the string

strutil.PadRight("lorem", 9, "<-") //-> "lorem<-<-"

Random (Docs)

Creates a random string from a character set

strutil.Random("abcdefghi", 10) //-> "aciafbeafg"

RemoveAccents (Docs)

Convert accented letters to ascii counterparts

strutil.RemoveAccents("résumé") //-> "resume"

ReplaceAllToOne (Docs)

Replace all substrings in the text with the specified string

strutil.ReplaceAllToOne("lorem ipsum", []string{"o","e","i","u"}, ".") //-> "l.r.m .ps.m"

Reverse (Docs)

Reverses the string

strutil.Reverse("lorem") //-> "merol"

Splice (Docs)

Replaces a part of the string

strutil.Splice("lorem", "-x-", 2, 3) //-> "lo-x-em"

SplitAndMap (Docs)

Splits the string and runs the function fn on every part

strutil.MapLines("lorem-ipsum-dolor", "-",  strutil.Reverse) //-> "merol\nmuspi\nrolod"

Slugify (Docs)

Converts the string to a slug

strutil.Slugify("Lorem ipsum, dolör") //-> "lorem-ipsum-dolor"

SlugifySpecial (Docs)

Converts the string to a slug with custom delimiter.

strutil.SlugifySpecial("Lorem ipsum, dolör", "_") //-> "lorem_ipsum_dolor"

SplitCamelCase (Docs)

Splits the words in a camelCase string

strutil.SplitCamelCase("loremIpsum") //-> []string{"lorem", "Ipsum"}

Substring (Docs)

Gets a part of the string without panics

strutil.SafeSubstring("lorem", 0, 1) //-> "l"

MustSubstring (Docs)

Gets a part of the string

strutil.Substring("lorem", 0, 1) //-> "l"

Summary (Docs)

Cuts the string to the specified length

strutil.Summary("Lorem ipsum dolor sit amet",  10, "...") //-> "lorem ipsum..."

Tile (Docs)

Repeats the pattern until the result reaches the 'length'

strutil.Tile("-৹", 4) //-> "-৹-৹"

ToSnakeCase (Docs)

Converts the string to snake_case

strutil.ToSnakeCase("Snake Case") //-> "snake_case"

ToCamelCase (Docs)

Converts the string to camelCase

strutil.ToCamelCase("Camel Case") //-> "camelCase"

Words (Docs)

Returns the words inside the text

strutil.Words("Lorem ipsum, dolor sit amet") //-> []string{"Lorem", "ipsum", "dolor", "sit", "amet"}

WordWrap (Docs)

Wraps the lines in the text

strutil.WordWrap("Lorem ipsum dolor sit amet", 15, false) //-> "Lorem ipsum\ndolor sit amet"

Install

Prequsities:

  • Go 1.10+

Install with

go get github.com/ozgio/strutil

# Functions

Align aligns string to the "alignTo" which should be one of - strutil.Center - strutil.Left - strutil.Right.
AlignCenter centers str.
AlignLeft aligns string to the left.
AlignRight aligns string to the right.
CenterText centers the text by adding spaces to the left and right.
CountWords count the words, It uses the same base function with 'Words' function.
DefaultBox9Slice defines the character object to use with "CustomBox".
DrawBox creates a frame with "content" in it.
DrawCustomBox creates a frame with "content" in it.
ExpandTabs converts tabs to the spaces.
Indent indents every line of string str with the left parameter Empty lines are indented too.
IsASCII checks if all the characters in string are in standard ASCII table It is taken from strings.Fields function.
MapLines runs function fn on every line of the string.
MustSubstring gets a part of the string between start and end.
OSNewLine returns operating system's default new line character.
Pad left and right pads a string str with leftPad and rightPad.
PadLeft left pads a string str with "pad".
PadRight right pads a string str with "pad".
Random creates new string based on strSet.
RemoveAccents removes accents from the string.
ReplaceAllToOne replaces every string in the "from" with the string "to".
Reverse reverses the string Copied from here https://stackoverflow.com/a/20225618/153570.
SimpleBox9Slice defines a character set to use with DrawCustomBox.
Slugify converts a string to a slug which is useful in URLs, filenames.
SlugifySpecial converts a string to a slug with the delimiter.
Splice insert a new string in place of the string between start and end indexes.
SplitAndMap splits the string and runs the function fn on every part.
SplitCamelCase splits and returns words in camelCase format.
Substring gets a part of the string between start and end.
Summary cuts the string to a new length and adds the "end" to itIt only breaks up the words by spaces.
Tile repeats the pattern until the result reaches the 'length' It returns empty string if the pattern is "" or length <= 0.
ToCamelCase converts string into camelCase formatted string after trimming it.
ToSnakeCase converts string into snake_case formatted string.
Words returns the words inside the text.
WordWrap wraps the given string str based on colLen as max line width.

# Constants

Align type to use with align function.
Align type to use with align function.
Align type to use with align function.

# Variables

Len is an alias of utf8.RuneCountInString which returns the number of runes in s.

# Structs

Box9Slice is used by DrawBox functions to draw frames around text content by defining the corner and edge characters.

# Type aliases

AlignType text align variable like center or left.