"strutil" provides string functions for go applications.
For documentation with examples see GoDoc
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"
Prequsities:
- Go 1.10+
Install with
go get github.com/ozgio/strutil