Reglue Go

Stringable

tests linux macos windows

Table Of Contents

Introduction

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
}

Available Methods

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:

After AfterLast Append
Basename Before BeforeLast
Between BetweenFirst Camel
Contains ContainsAll Dirname
Dump EndsWith Exactly
Explode Headline IsEmpty
IsNotEmpty Kebab Lcfirst
Length Limit Lower
Ltrim NewLine Of
PadBoth PadLeft PadRight
Pipe Prepend Remove
Repeat Replace ReplaceArray
ReplaceFirst Rtrim Slug
Snake StartsWith String
Studly Substr Title
ToBoolean ToDate ToFloat
ToInteger ToString Trim
Ucfirst Unless Upper
Value When WhenContains
WhenContainsAll WhenEmpty WhenEndsWith
WhenExactly WhenNotEmpty WhenNotExactly
WhenStartsWith Wrap ReplaceLast
ReplaceNth    

Method Listing

ReplaceNth

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"

ReplaceLast

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"

Wrap

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"

WhenStartsWith

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"

WhenNotExactly

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"

WhenNotEmpty

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"

WhenExactly

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"

WhenEndsWith

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"

WhenEmpty

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"

WhenContainsAll

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"

WhenContains

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"

When

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"

Value

The Value method returns the underlying string value:

value := stringable.New("This is my name").Value()
// "This is my name"

Upper

The Upper method converts the given string to uppercase:

value := stringable.New("golang").Upper().Value()
// GOLANG

Unless

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"

Ucfirst

The Ucfirst method returns the given string with the first character capitalized:

value := stringable.New("foo bar").Ucfirst()
// "Foo bar"

Trim

The Trim method trims the given string:

value := stringable.New("$day$").Trim().Value()
// "day"

ToString

The ToString method returns the underlying string value:

ToInteger

The ToInteger method returns an integer value from string:

value := stringable.New("99").ToInteger()
// 99

ToFloat

The ToFloat method returns a float value from string:

value := stringable.New("9.9").ToFloat()
// 9.9

ToDate

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

ToBoolean

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

Title

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"

Substr

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"

Studly

The Studly method converts the given string to StudlyCase:

value := stringable.New("foo_bar").Studly().Value()
// "FooBar"

String

The String method returns the underlying string value:

StartsWith

The StartsWith method determines if the given string begins with the given value:

value := stringable.New("This is my name").StartsWith("This")
// true

Snake

The Snake method converts the given string to snake_case:

value := stringable.New("fooBar").Snake().Value()
// "foo_bar"

Slug

The Slug method generates a URL friendly “slug” from the given string:

value := stringable.New("Reglue Framework").Slug("-").Value()
// "reglue-framework"

Rtrim

The Rtrim method trims the right side of the given string:

value := stringable.New("$day$").Rtrim().Value()
// "$day"

ReplaceFirst

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"

ReplaceArray

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"

Replace

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"

Repeat

The Repeat method repeats the given string:

value := stringable.New("a").Repeat(5).Value()
// "aaaaa"

Remove

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!"

Prepend

The Prepend method prepends the given values onto the string:

value := stringable.New("Go!").Prepend(" ", "Awesome").Value()
// "Awesome Go!"

Pipe

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"

PadRight

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___"

PadLeft

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"

PadBoth

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___"

Of

The Of method creates a new stringable instance:

value := stringable.New("awesome").Of("Framework").Value()
// "Framework"

NewLine

The NewLine method appends an “end of line” character to a string:

value := stringable.New("Reglue").NewLine().Append("Framework").Value()
// "Reglue"
// "Framework"

Ltrim

The Ltrim method trims the left side of the string:

value := stringable.New("$day$").Ltrim().Value()
// "day$"

Lower

The Lower method converts the given string to lowercase:

value := stringable.New("GOLANG").Lower().Value()
// golang

Limit

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 ***"

Length

The Length method returns the length of the given string:

value := stringable.New("Golang").Length()
// 6

Lcfirst

The Lcfirst method returns the given string with the first character lowercased:

value := stringable.New("Foo Bar").Lcfirst()
// "foo Bar"

Kebab

The Kebab method converts the given string to kebab-case:

value := stringable.New("fooBar").Kebab()
// "foo-bar"

IsNotEmpty

The IsNotEmpty method determines if the given string is not empty:

value := stringable.New(" ").IsNotEmpty()
// false
value := stringable.New("Golang").IsNotEmpty()
// true

IsEmpty

The IsEmpty method determines if the given string is empty:

value := stringable.New(" ").IsEmpty()
// true
value := stringable.New("Golang").IsEmpty()
// false

Headline

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"

Explode

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"]

Exactly

The Exactly method determines if the given string is an exact match with another string:

value := stringable.New("Golang").Exactly("Golang")
// true

EndsWith

The EndsWith method determines if the given string ends with the given value:

value := stringable.New("This is my name").EndsWith("name")
// true

Dump

The Dump method writes the string to standard output:

value := stringable.New("Write to standard output").Dump().Value()
// "Write to standard output"

Dirname

The Dirname method returns the parent directory portion of the given string:

value := stringable.New("/foo/bar/baz").Dirname().Value()
// "/foo/bar"

ContainsAll

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

Contains

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

Camel

The Camel method converts the given string to camelCase:

value := stringable.New("foo_bar").Camel().Value()
// "fooBar"

BetweenFirst

The BetweenFirst method returns the smallest possible portion of a string between two values:

value := stringable.New("[a] bc [d]").BetweenFirst("[", "]").Value()
// "a"

Between

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 "

BeforeLast

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 "

Before

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 "

Basename

The Basename method will return the trailing name component of the given string:

value := stringable.New("/foo/bar/baz").Basename().Value()
// "baz"

Append

The Append method appends the given values to the string:

value := stringable.New("Paula").Append(" ", "White").Value()
// "Paula White"

AfterLast

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"

After

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"

Unit Tests Matrix

This package has been tested on the following platforms.

golang ubuntu macos windows
1.18.x
1.20.x
1.22.x
Go logo