Table Of Contents
The stringable package provides an expressive chainable interface for working with string values.
package main
import "github.com/reglue4go/stringable"
func main() {
title := stringable.New("go").Append("lang").Ucfirst().String()
fmt.Printf("%v\n", title) // Golang
}
Each method available on the stringable instance may be chained to fluently manipulate the underlying value. Almost every method returns a new stringable instance, allowing you to preserve the original copy of the value when necessary:
The ReplaceNth method replaces the nth occurrence of a given value in a string:
value := stringable.New("the fox jumps over the lazy dog").ReplaceNth("the", "A", 1).Value()
// "A fox jumps over the lazy dog"
value := stringable.New("the fox jumps over the lazy dog").ReplaceNth("the", "a", 2).Value()
// "the fox jumps over a lazy dog"
The ReplaceLast method replaces the last occurrence of a given value in a string:
value := stringable.New("the fox jumps over the lazy dog").ReplaceLast("the", "a").Value()
// "the fox jumps over a lazy dog"
The Wrap method wraps the given string with an additional string or pair of strings:
value := stringable.New("is").Wrap("This ", " golang")
// "This is golang"
The WhenStartsWith method invokes the given closure if the string starts with the given sub-string. The closure will receive the string value:
value := stringable.New("disney world").
WhenStartsWith("disney", func(value string) string {
return stringable.New(value).Title().String();
}).Value()
// "Disney World"
The WhenNotExactly method invokes the given closure if the string does not exactly match the given string. The closure will receive the string value
value := stringable.New("alita").
WhenNotExactly("Alita", func(value string) string {
return stringable.New(value).Title().String();
}).Value()
// "Alita"
The WhenNotEmpty method invokes the given closure if the string is not empty. If the closure returns a value, that value will also be returned by the whenNotEmpty method. If the closure does not return a value, the fluent string instance will be returned:
value := stringable.New("Framework").
WhenNotEmpty(func(value string) string {
return stringable.New(value).Prepend("Gin ").String();
}).Value()
// "Gin Framework"
The WhenExactly method invokes the given closure if the string exactly matches the given string. The closure will receive the string value:
value := stringable.New("Alita").
WhenExactly("Alita", func(value string) string {
return "99. " + value;
}).Value()
// "99. Alita"
The WhenEndsWith method invokes the given closure if the string ends with the given sub-string. The closure will receive the string value:
value := stringable.New("disney world").
WhenEndsWith("world", func(value string) string {
return stringable.New(value).Title().String();
}).Value()
// "Disney World"
The WhenEmpty method invokes the given closure if the string is empty. If the closure returns a value, that value will also be returned by the whenEmpty method. If the closure does not return a value, the fluent string instance will be returned:
value := stringable.New(" ").
WhenEmpty(func(value string) string {
return "Avenger"
}).Value()
// "Avenger"
The WhenContainsAll method invokes the given closure if the string contains all of the given sub-strings. The closure will receive the string value:
value := stringable.New("tony stark").
WhenContainsAll([]string{"tony", "stark"}, func(value string) string {
return stringable.New(value).Title().String();
}).Value()
// "Tony Stark"
The WhenContains method invokes the given closure if the string contains the given value. The closure will receive the string value:
value := stringable.New("tony stark").
WhenContains("tony", func(value string) string {
return stringable.New(value).Title().String();
}).Value()
// "Tony Stark"
The When method invokes the given closure if a given condition is true. The closure will receive the string value:
value := stringable.New("Birds").
When(true, func(value string) string {
return "Angry " + value
}).Value()
// "Angry Birds"
value := stringable.New("Birds").
When(false, func(value string) string {
return "Angry " + value
}, func(value string) string {
return "Happy " + value
}).Value()
// "Happy Birds"
The Value method returns the underlying string value:
value := stringable.New("This is my name").Value()
// "This is my name"
The Upper method converts the given string to uppercase:
value := stringable.New("golang").Upper().Value()
// GOLANG
The Unless method applies the callback if the given “value” is (or resolves to) falsy.
value := stringable.New("Birds").
Unless(false, func(value string) string {
return "Angry " + value
}).Value()
// "Angry Birds"
value := stringable.New("Birds").
Unless(true, func(value string) string {
return "Angry " + value
}, func(value string) string {
return "Happy " + value
}).Value()
// "Happy Birds"
The Ucfirst method returns the given string with the first character capitalized:
value := stringable.New("foo bar").Ucfirst()
// "Foo bar"
The Trim method trims the given string:
value := stringable.New("$day$").Trim().Value()
// "day"
The ToString method returns the underlying string value:
The ToInteger method returns an integer value from string:
value := stringable.New("99").ToInteger()
// 99
The ToFloat method returns a float value from string:
value := stringable.New("9.9").ToFloat()
// 9.9
The ToDate method returns a date time instance from string value:
value := stringable.New("now").ToDate().Format("2006-01-02 15:04:05")
// 2024-03-29 12:22:35.913399 +0400 +04 m=+0.000832834
The ToBoolean method returns the underlying string value:
value := stringable.New("true").ToBoolean()
value := stringable.New("True").ToBoolean()
// true
value := stringable.New("false").ToBoolean()
value := stringable.New("False").ToBoolean()
value := stringable.New("invalid").ToBoolean()
// false
The Title method converts the given string to Title Case:
value := stringable.New("a nice title uses the correct case").Title().Value()
// "A Nice Title Uses The Correct Case"
The Substr method returns the portion of the string specified by the given start and length parameters:
value := stringable.New("Boney James").Substr(0, 5).Value()
// "Boney"
value := stringable.New("Boney James").Substr(-5, 0).Value()
// "James"
The Studly method converts the given string to StudlyCase:
value := stringable.New("foo_bar").Studly().Value()
// "FooBar"
The String method returns the underlying string value:
The StartsWith method determines if the given string begins with the given value:
value := stringable.New("This is my name").StartsWith("This")
// true
The Snake method converts the given string to snake_case:
value := stringable.New("fooBar").Snake().Value()
// "foo_bar"
The Slug method generates a URL friendly “slug” from the given string:
value := stringable.New("Reglue Framework").Slug("-").Value()
// "reglue-framework"
The Rtrim method trims the right side of the given string:
value := stringable.New("$day$").Rtrim().Value()
// "$day"
The ReplaceFirst method replaces the first occurrence of a given value in a string:
value := stringable.New("the quick brown fox jumps over the lazy dog").ReplaceFirst("the", "a").Value()
// "a quick brown fox jumps over the lazy dog"
The ReplaceArray method replaces a given value in the string sequentially using an array:
value := stringable.New("The event will take place between ? and ?").ReplaceArray("?", "8:30", "9:00").Value()
// "The event will take place between 8:30 and 9:30"
The Replace method replaces a given string within the string:
value := stringable.New("Golang 1.18.x").Replace("18.x", "22.x").Value()
// "Golang 1.22.x"
The Repeat method repeats the given string:
value := stringable.New("a").Repeat(5).Value()
// "aaaaa"
The Remove method removes the given value or array of values from the string:
value := stringable.New("Go is quite beautiful!").Remove("quite").Value()
// "Go is beautiful!"
The Prepend method prepends the given values onto the string:
value := stringable.New("Go!").Prepend(" ", "Awesome").Value()
// "Awesome Go!"
The Pipe method allows you to transform the string by passing its current value to the given callable:
value := stringable.New("Angry").
Pipe(func(value string) string {
return value + " Birds"
}).Value()
// "Angry Birds"
The PadRight method pads the right side of a string with another string until the final string reaches the desired length:
value := stringable.New("James").PadRight("10, '_'").Value()
// "James___"
The PadLeft method pads the left side of a string with another string until the final string reaches the desired length
value := stringable.New("James").PadLeft("10, '_'").Value()
// "__James"
The PadBoth method pads both sides of a string with another string until the final string reaches the desired length:
value := stringable.New("James").PadBoth("10, '_'").Value()
// "__James___"
The Of method creates a new stringable instance:
value := stringable.New("awesome").Of("Framework").Value()
// "Framework"
The NewLine method appends an “end of line” character to a string:
value := stringable.New("Reglue").NewLine().Append("Framework").Value()
// "Reglue"
// "Framework"
The Ltrim method trims the left side of the string:
value := stringable.New("$day$").Ltrim().Value()
// "day$"
The Lower method converts the given string to lowercase:
value := stringable.New("GOLANG").Lower().Value()
// golang
The Limit method truncates the given string to the specified length:
value := stringable.New("The quick brown fox jumps over the lazy dog").Limit(20)
// "The quick brown fox..."
value := stringable.New("The quick brown fox jumps over the lazy dog").Limit(20, " ***")
// "The quick brown fox ***"
The Length method returns the length of the given string:
value := stringable.New("Golang").Length()
// 6
The Lcfirst method returns the given string with the first character lowercased:
value := stringable.New("Foo Bar").Lcfirst()
// "foo Bar"
The Kebab method converts the given string to kebab-case:
value := stringable.New("fooBar").Kebab()
// "foo-bar"
The IsNotEmpty method determines if the given string is not empty:
value := stringable.New(" ").IsNotEmpty()
// false
value := stringable.New("Golang").IsNotEmpty()
// true
The IsEmpty method determines if the given string is empty:
value := stringable.New(" ").IsEmpty()
// true
value := stringable.New("Golang").IsEmpty()
// false
The Headline method will convert strings delimited by casing, hyphens, or underscores into a space delimited string with each word’s first letter capitalized:
value := stringable.New("Email-Notification_Sent").Headline()
// "Email Notification Sent"
value := stringable.New("jane_doe").Headline(language.BritishEnglish)
// "Jane Doe"
The Explode method splits the string by the given delimiter and returns a collection containing each section of the split string:
value := stringable.New("foo bar baz").Explode(" ")
// ["foo" "bar" "baz"]
The Exactly method determines if the given string is an exact match with another string:
value := stringable.New("Golang").Exactly("Golang")
// true
The EndsWith method determines if the given string ends with the given value:
value := stringable.New("This is my name").EndsWith("name")
// true
The Dump method writes the string to standard output:
value := stringable.New("Write to standard output").Dump().Value()
// "Write to standard output"
The Dirname method returns the parent directory portion of the given string:
value := stringable.New("/foo/bar/baz").Dirname().Value()
// "/foo/bar"
The ContainsAll method determines if the given string contains all of the values in the given array:
value := stringable.New("This is my name").ContainsAll("my", "name")
// true
The Contains method determines if the given string contains the given value. This method is case sensitive:
value := stringable.New("This is my name").Contains("my")
// true
The Camel method converts the given string to camelCase:
value := stringable.New("foo_bar").Camel().Value()
// "fooBar"
The BetweenFirst method returns the smallest possible portion of a string between two values:
value := stringable.New("[a] bc [d]").BetweenFirst("[", "]").Value()
// "a"
The Between method returns the portion of a string between two values:
value := stringable.New("This is my name").Between("This", "name").Value()
// " is my "
The BeforeLast method returns everything before the last occurrence of the given value in a string:
value := stringable.New("This is my name").BeforeLast("is").Value()
// "This "
The Before method returns everything before the given value in a string:
value := stringable.New("This is my name").Before("my name").Value()
// "This is "
The Basename method will return the trailing name component of the given string:
value := stringable.New("/foo/bar/baz").Basename().Value()
// "baz"
The Append method appends the given values to the string:
value := stringable.New("Paula").Append(" ", "White").Value()
// "Paula White"
The AfterLast method method returns everything after the last occurrence of the given value in a string. The entire string will be returned if the value does not exist within the string:
value := stringable.New("internal.controllers.users.controller").AfterLast(".").Value()
// "controller"
The After method method returns everything after the given value in a string. The entire string will be returned if the value does not exist within the string:
value := stringable.New("This is my name").After("This is").Value()
// " my name"
This package has been tested on the following platforms.
golang | ubuntu | macos | windows |
---|---|---|---|
1.18.x | ✓ | ✓ | ✓ |
1.20.x | ✓ | ✓ | ✓ |
1.22.x | ✓ | ✓ | ✓ |