Convert string to camel case, snake case, kebab case / slugify, custom delimiter, pad string, tease string and many other functionality with help of by Stringy package. You can convert camelcase to snakecase or kebabcase, or snakecase to camelcase and kebabcase and vice versa. This package was inspired from PHP danielstjules/Stringy.
Golang has very rich strings core package despite some extra helper function are not available and this stringy package is here to fill that void. Plus there are other some packages in golang, that have same functionality but for some extreme cases they fail to provide correct output. This package cross flexibility is it's main advantage. You can convert to camelcase to snakecase or kebabcase or vice versa.
package main
import (
"fmt"
"github.com/gobeam/stringy"
)
func main() {
str := stringy.New("hello__man how-Are you??")
result := str.CamelCase("?", "")
fmt.Println(result) // HelloManHowAreYou
snakeStr := str.SnakeCase("?", "")
fmt.Println(snakeStr.ToLower()) // hello_man_how_are_you
kebabStr := str.KebabCase("?", "")
fmt.Println(kebabStr.ToUpper()) // HELLO-MAN-HOW-ARE-YOU
}$ go get -u -v github.com/gobeam/stringyor with dep
$ dep ensure -add github.com/gobeam/stringyBetween takes two string params start and end which and returns value which is in middle of start and end part of input. You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
strBetween := stringy.New("HelloMyName")
fmt.Println(strBetween.Between("hello", "name").ToUpper()) // MYBoolean func returns boolean value of string value like on, off, 0, 1, yes, no returns boolean value of string input. You can chain this function on other function which returns implemented StringManipulation interface.
boolString := stringy.New("off")
fmt.Println(boolString.Boolean()) // falseCamelCase is variadic function which takes one Param rule i.e slice of strings and it returns input type string in camel case form and rule helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you dont have to worry about it.
camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
fmt.Println(camelCase.CamelCase("?", "", "#", "")) // thisIsOneMessedUpStringCanWeReallyCamelCaseItlook how it omitted ?## from string. If you dont want to omit anything and since it returns plain strings and you cant actually cap all or lower case all camelcase string its not required.
camelCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
fmt.Println(camelCase.CamelCase()) // thisIsOneMessedUpStringCanWeReallyCamelCaseIt?##Contains checks if the string contains the specified substring and returns a boolean value. This is a wrapper around Go's standard strings.Contains function that fits into the Stringy interface.
str := stringy.New("Hello World")
fmt.Println(str.Contains("World")) // true
fmt.Println(str.Contains("Universe")) // falseContainsAll is variadic function which takes slice of strings as param and checks if they are present in input and returns boolean value accordingly.
contains := stringy.New("hello mam how are you??")
fmt.Println(contains.ContainsAll("mam", "?")) // trueDelimited is variadic function that takes two params delimiter and slice of strings named rule. It joins the string by passed delimeter. Rule param helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you dont have to worry about it. If you don't want to omit any character pass empty string.
delimiterString := stringy.New("ThisIsOne___messed up string. Can we Really delimeter-case It?")
fmt.Println(delimiterString.Delimited("?").Get())You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
First returns first n characters from provided input. It removes all spaces in string before doing so.
fcn := stringy.New("4111 1111 1111 1111")
first := fcn.First(4)
fmt.Println(first) // 4111Get simply returns result and can be chained on function which returns StringManipulation interface view above examples
getString := stringy.New("hello roshan")
fmt.Println(getString.Get()) // hello roshanIsEmpty checks if the string is empty or contains only whitespace characters. It returns true for empty strings or strings containing only spaces, tabs, or newlines.
emptyStr := stringy.New("")
fmt.Println(emptyStr.IsEmpty()) // true
whitespaceStr := stringy.New(" \t\n")
fmt.Println(whitespaceStr.IsEmpty()) // true
normalStr := stringy.New("Hello")
fmt.Println(normalStr.IsEmpty()) // false
emptyStr := stringy.New("")
fmt.Println(emptyStr.IsEmpty()) // true
whitespaceStr := stringy.New(" \t\n")
fmt.Println(whitespaceStr.IsEmpty()) // true
normalStr := stringy.New("Hello")
fmt.Println(normalStr.IsEmpty()) // false
#### KebabCase(rule ...string) StringManipulation
KebabCase/slugify is variadic function that takes one Param slice of strings named rule and it returns passed string in kebab case or slugify form. Rule param helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you don't have to worry about it. If you don't want to omit any character pass nothing.
```go
str := stringy.New("hello__man how-Are you??")
kebabStr := str.KebabCase("?","")
fmt.Println(kebabStr.ToUpper()) // HELLO-MAN-HOW-ARE-YOU
fmt.Println(kebabStr.Get()) // hello-man-how-Are-youYou can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
Last returns last n characters from provided input. It removes all spaces in string before doing so.
lcn := stringy.New("4111 1111 1111 1348")
last := lcn.Last(4)
fmt.Println(last) // 1348LcFirst simply returns result by lower casing first letter of string and it can be chained on function which return StringManipulation interface
contains := stringy.New("Hello roshan")
fmt.Println(contains.LcFirst()) // hello roshanLines returns slice of strings by removing white space characters
lines := stringy.New("fòô\r\nbàř\nyolo123")
fmt.Println(lines.Lines()) // [fòô bàř yolo123]Pad takes three param length i.e total length to be after padding, with i.e what to pad with and pad type which can be ("both" or "left" or "right") it return string after padding upto length by with param and on padType type it can be chained on function which return StringManipulation interface
pad := stringy.New("Roshan")
fmt.Println(pad.Pad(0, "0", "both")) // 00Roshan00
fmt.Println(pad.Pad(0, "0", "left")) // 0000Roshan
fmt.Println(pad.Pad(0, "0", "right")) // Roshan0000RemoveSpecialCharacter removes all special characters and returns the string nit can be chained on function which return StringManipulation interface
cleanString := stringy.New("special@#remove%%%%")
fmt.Println(cleanString.RemoveSpecialCharacter()) // specialremoveReplaceFirst takes two param search and replace. It returns string by searching search sub string and replacing it with replace substring on first occurrence it can be chained on function which return StringManipulation interface.
replaceFirst := stringy.New("Hello My name is Roshan and his name is Alis.")
fmt.Println(replaceFirst.ReplaceFirst("name", "nombre")) // Hello My nombre is Roshan and his name is Alis.ReplaceAll replaces all occurrences of a search string with a replacement string. It complements the existing ReplaceFirst and ReplaceLast methods and provides a chainable wrapper around Go's strings.ReplaceAll function.
go str := stringy.New("Hello World World")
fmt.Println(str.ReplaceAll("World", "Universe").Get()) // Hello Universe Universe
// Chain with other methods
fmt.Println(str.ReplaceAll("World", "Universe").ToUpper()) // HELLO UNIVERSE UNIVERSEReplaceLast takes two param search and replace it return string by searching search sub string and replacing it with replace substring on last occurrence it can be chained on function which return StringManipulation interface
replaceLast := stringy.New("Hello My name is Roshan and his name is Alis.")
fmt.Println(replaceLast.ReplaceLast("name", "nombre")) // Hello My name is Roshan and his nombre is Alis.Reverse function reverses the passed strings it can be chained on function which return StringManipulation interface.
reverse := stringy.New("This is only test")
fmt.Println(reverse.Reverse()) // tset ylno si sihTSentenceCase is a variadic function that takes one parameter: slice of strings named rule. It converts text from various formats (camelCase, snake_case, kebab-case, etc.) to sentence case format, where the first word is capitalized and the rest are lowercase, with words separated by spaces. Rule parameter helps to omit characters you want to omit from the string. By default, special characters like "_", "-", ".", " " are treated as word separators.
str := stringy.New("thisIsCamelCase_with_snake_too")
fmt.Println(str.SentenceCase().Get()) // This is camel case with snake too
mixedStr := stringy.New("THIS-IS-KEBAB@and#special&chars")
fmt.Println(mixedStr.SentenceCase("@", " ", "#", " ", "&", " ").Get()) // This is kebab and special charsYou can chain ToUpper which will make the result all uppercase or Get which will return the result as it is. The first word is automatically capitalized, and all other words are lowercase.
Shuffle shuffles the given string randomly it can be chained on function which return StringManipulation interface.
shuffleString := stringy.New("roshan")
fmt.Println(shuffleString.Shuffle()) // nhasroSurround takes one param with which is used to surround user input and it can be chained on function which return StringManipulation interface.
surroundStr := stringy.New("__")
fmt.Println(surroundStr.Surround("-")) // -__-SnakeCase is variadic function that takes one Param slice of strings named rule and it returns passed string in snake case form. Rule param helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you don't have to worry about it. If you don't want to omit any character pass nothing.
snakeCase := stringy.New("ThisIsOne___messed up string. Can we Really Snake Case It?")
fmt.Println(snakeCase.SnakeCase("?", "").Get()) // This_Is_One_messed_up_string_Can_we_Really_Snake_Case_It
fmt.Println(snakeCase.SnakeCase("?", "").ToUpper()) // THIS_IS_ONE_MESSED_UP_STRING_CAN_WE_REALLY_SNAKE_CASE_ITYou can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
Substring extracts part of a string from the start position (inclusive) to the end position (exclusive). It handles multi-byte characters correctly and has safety checks for out-of-bounds indices.
// Basic usage
go str := stringy.New("Hello World")
fmt.Println(str.Substring(0, 5).Get()) // Hello
fmt.Println(str.Substring(6, 11).Get()) // World
// With multi-byte characters
str = stringy.New("Hello 世界")
fmt.Println(str.Substring(6, 8).Get()) // 世界Tease takes two params length and indicator and it shortens given string on passed length and adds indicator on end it can be chained on function which return StringManipulation interface.
teaseString := stringy.New("Hello My name is Roshan. I am full stack developer")
fmt.Println(teaseString.Tease(20, "...")) // Hello My name is Ros...Title returns string with first letter of each word in uppercase it can be chained on function which return StringManipulation interface.
title := stringy.New("hello roshan")
fmt.Println(title.Title()) // Hello RoshanToLower makes all string of user input to lowercase and it can be chained on function which return StringManipulation interface.
snakeCase := stringy.New("ThisIsOne___messed up string. Can we Really Snake Case It?")
fmt.Println(snakeCase.SnakeCase("?", "").ToLower()) // this_is_one_messed_up_string_can_we_really_snake_case_itTrim removes leading and trailing whitespace or specified characters from the string. If no characters are specified, it trims whitespace by default. It can be chained with other methods that return StringManipulation interface.
trimString := stringy.New(" Hello World ")
fmt.Println(trimString.Trim().Get()) // Hello World
specialTrim := stringy.New("!!!Hello World!!!")
fmt.Println(specialTrim.Trim("!").Get()) // Hello World
chainedTrim := stringy.New(" hello world ")
fmt.Println(chainedTrim.Trim().UcFirst()) // Hello worldYou can chain ToUpper which will make the result all uppercase, ToLower which will make the result all lowercase, or Get which will return the result as it is.
ToUpper makes all string of user input to uppercase and it can be chained on function which return StringManipulation interface.
snakeCase := stringy.New("ThisIsOne___messed up string. Can we Really Snake Case It?")
fmt.Println(snakeCase.SnakeCase("?", "").ToUpper()) // THIS_IS_ONE_MESSED_UP_STRING_CAN_WE_REALLY_SNAKE_CASE_ITUcFirst simply returns result by upper casing first letter of string and it can be chained on function which return StringManipulation interface.
contains := stringy.New("hello roshan")
fmt.Println(contains.UcFirst()) // Hello roshanPrefix makes sure string has been prefixed with a given string and avoids adding it again if it has.
ufo := stringy.New("known flying object")
fmt.Println(ufo.Prefix("un")) // unknown flying objectSuffix makes sure string has been suffixed with a given string and avoids adding it again if it has.
pun := stringy.New("this really is a cliff")
fmt.Println(pun.Suffix("hanger")) // this really is a cliffhangerSlugifyWithCount(count int) StringManipulation SlugifyWithCount creates a URL-friendly slug with an optional uniqueness counter appended. This is useful for creating unique URL slugs for blog posts, articles, or database entries.
Acronym func returns acronym of input string. You can chain ToUpper() which with make result all upercase or ToLower() which will make result all lower case or Get which will return result as it is
acronym := stringy.New("Laugh Out Loud")
fmt.Println(acronym.Acronym().ToLower()) // lolPascalCase is variadic function which takes one Param rule i.e slice of strings and it returns input type string in pascal case form and rule helps to omit character you want to omit from string. By default special characters like "_", "-","."," " are treated like word separator and treated accordingly by default and you don't have to worry about it.
pascalCase := stringy.New("ThisIsOne___messed up string. Can we Really pascal-case It ?##")
fmt.Println(pascalCase.PascalCase("?", "", "#", "")) // ThisIsOneMessedUpStringCanWeReallyPascalCaseItlook how it omitted ?## from string. If you dont want to omit anything and since it returns plain strings and you cant actually cap all or lower case all camelcase string it's not required.
pascalCase := stringy.New("ThisIsOne___messed up string. Can we Really camel-case It ?##")
fmt.Println(pascalCase.PascalCase()) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt?##SlugifyWithCount creates a URL-friendly slug with an optional uniqueness counter appended. This is useful for creating unique URL slugs for blog posts, articles, or database entries.
slug := stringy.New("Hello World")
fmt.Println(slug.SlugifyWithCount(1).Get()) // hello-world-1
fmt.Println(slug.SlugifyWithCount(2).ToUpper()) // HELLO-WORLD-2TruncateWords truncates the string to a specified number of words and appends a suffix. This is useful for creating previews or summaries of longer text.
truncate := stringy.New("This is a long sentence that needs to be truncated.")
fmt.Println(truncate.TruncateWords(5, "...").Get()) // This is a long sentence...
fmt.Println(truncate.TruncateWords(3, "...").ToUpper()) // THIS IS A LONG...WordCount returns the number of words in the string. It uses whitespace as the word separator and can be chained with other methods.
wordCount := stringy.New("Hello World")
fmt.Println(wordCount.WordCount()) // 2
multiByteCount := stringy.New("Hello 世界")
fmt.Println(multiByteCount.WordCount()) // 2Substring extracts part of a string from the start position (inclusive) to the end position (exclusive). It handles multi-byte characters correctly and has safety checks for out-of-bounds indices.
// Basic usage
str := stringy.New("Hello World")
fmt.Println(str.Substring(0, 5).Get()) // Hello
fmt.Println(str.Substring(6, 11).Get()) // World
// With multi-byte characters
str = stringy.New("Hello 世界")
fmt.Println(str.Substring(6, 8).Get()) // 世界Contains checks if the string contains the specified substring and returns a boolean value. This is a wrapper around Go's standard strings.Contains function that fits into the Stringy interface.
str := stringy.New("Hello World")
fmt.Println(str.Contains("World")) // true
fmt.Println(str.Contains("Universe")) // falseReplaceAll replaces all occurrences of a search string with a replacement string. It complements the existing ReplaceFirst and ReplaceLast methods and provides a chainable wrapper around Go's strings.ReplaceAll function.
str := stringy.New("Hello World World")
fmt.Println(str.ReplaceAll("World", "Universe").Get()) // Hello Universe Universe
// Chain with other methods
fmt.Println(str.ReplaceAll("World", "Universe").ToUpper()) // HELLO UNIVERSE UNIVERSE$ go testPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate. - see CONTRIBUTING.md for details.
Released under the MIT License - see LICENSE.txt for details.