A fluent, Laravel-inspired string toolkit for Go, focused on rune-safe helpers, expressive transformations, and predictable behavior beyond the standard library.
go get github.com/goforj/strEvery function has a corresponding runnable example under ./examples.
These examples are generated directly from the documentation blocks of each function, ensuring the docs and code never drift. These are the same examples you see here in the README and GoDoc.
An automated test executes every example to verify it builds and runs successfully.
This guarantees all examples are valid, up-to-date, and remain functional as the API evolves.
ChopEnd removes the first matching suffix if present.
v := str.Of("file.txt").ChopEnd(".txt", ".md").String()
godump.Dump(v)
// #string fileChopStart removes the first matching prefix if present.
v := str.Of("https://goforj.dev").ChopStart("https://", "http://").String()
godump.Dump(v)
// #string goforj.devEnsurePrefix ensures the string starts with prefix, adding it if missing.
v := str.Of("path/to").EnsurePrefix("/").String()
godump.Dump(v)
// #string /path/toEnsureSuffix ensures the string ends with suffix, adding it if missing.
v := str.Of("path/to").EnsureSuffix("/").String()
godump.Dump(v)
// #string path/to/Unwrap removes matching before and after strings if present.
v := str.Of("\"GoForj\"").Unwrap("\"", "\"").String()
godump.Dump(v)
// #string GoForjWrap surrounds the string with before and after (after defaults to before).
v := str.Of("GoForj").Wrap("\"", "").String()
godump.Dump(v)
// #string "GoForj"Camel converts the string to camelCase.
v := str.Of("foo_bar baz").Camel().String()
godump.Dump(v)
// #string fooBarBazHeadline converts the string into a human-friendly headline: splits on case/underscores/dashes/whitespace, title-cases words, and lowercases small words (except the first).
v := str.Of("emailNotification_sent").Headline().String()
godump.Dump(v)
// #string Email Notification SentKebab converts the string to kebab-case.
v := str.Of("fooBar baz").Kebab().String()
godump.Dump(v)
// #string foo-bar-bazLcFirst returns the string with the first rune lower-cased.
v := str.Of("Gopher")
godump.Dump(v)
// #string gopherPascal converts the string to PascalCase.
v := str.Of("foo_bar baz")
godump.Dump(v)
// #string FooBarBazSnake converts the string to snake_case using the provided separator (default "_").
v := str.Of("fooBar baz").Snake("_").String()
godump.Dump(v)
// #string foo_bar_bazTitle converts the string to title case (first letter of each word upper, rest lower) using Unicode rules.
v := str.Of("a nice title uses the correct case").Title().String()
godump.Dump(v)
// #string A Nice Title Uses The Correct CaseToLower returns a lowercase copy of the string using Unicode rules.
v := str.Of("GoLang").ToLower().String()
godump.Dump(v)
// #string golangToTitle returns a title-cased copy where all letters are mapped using Unicode title case.
v := str.Of("ß").ToTitle().String()
godump.Dump(v)
// #string SSToUpper returns an uppercase copy of the string using Unicode rules.
v := str.Of("GoLang").ToUpper().String()
godump.Dump(v)
// #string GOLANGUcFirst returns the string with the first rune upper-cased.
v := str.Of("gopher").UcFirst().String()
godump.Dump(v)
// #string GopherUcWords uppercases the first rune of each word, leaving the rest unchanged. Words are sequences of letters/digits.
v := str.Of("hello WORLD").UcWords().String()
godump.Dump(v)
// #string Hello WORLDIsASCII reports whether the string consists solely of 7-bit ASCII runes.
v := str.Of("gopher").IsASCII()
godump.Dump(v)
// #bool trueIsBlank reports whether the string contains only Unicode whitespace.
v := str.Of(" \\t\\n")
godump.Dump(v.IsBlank())
// #bool trueIsEmpty reports whether the string has zero length.
v := str.Of("").IsEmpty()
godump.Dump(v)
// #bool trueDeduplicate collapses consecutive instances of char into a single instance. If char is zero, space is used.
v := str.Of("The Go Playground").Deduplicate(' ').String()
godump.Dump(v)
// #string The Go PlaygroundSquish trims leading/trailing whitespace and collapses internal whitespace to single spaces.
v := str.Of(" go forj ").Squish().String()
godump.Dump(v)
// #string go forjTrim trims leading and trailing characters. If cutset is empty, trims Unicode whitespace.
v := str.Of(" GoForj ").Trim("").String()
godump.Dump(v)
// #string GoForjTrimLeft trims leading characters. If cutset is empty, trims Unicode whitespace.
v := str.Of(" GoForj ").TrimLeft("").String()
godump.Dump(v)
// #string GoForjTrimRight trims trailing characters. If cutset is empty, trims Unicode whitespace.
v := str.Of(" GoForj ").TrimRight("").String()
godump.Dump(v)
// #string GoForjEquals reports whether the string exactly matches other (case-sensitive).
v := str.Of("gopher").Equals("gopher")
godump.Dump(v)
// #bool trueEqualsFold reports whether the string matches other using Unicode case folding.
v := str.Of("gopher").EqualsFold("GOPHER")
godump.Dump(v)
// #bool trueAppend concatenates the provided parts to the end of the string.
v := str.Of("Go").Append("Forj", "!").String()
godump.Dump(v)
// #string GoForj!NewLine appends a newline character to the string.
v := str.Of("hello").NewLine().Append("world").String()
godump.Dump(v)
// #string hello\nworldPrepend concatenates the provided parts to the beginning of the string.
v := str.Of("World").Prepend("Hello ", "Go ").String()
godump.Dump(v)
// #string Hello Go WorldFromBase64 decodes a standard Base64 string.
v, err := str.Of("Z29waGVy").FromBase64()
godump.Dump(v.String(), err)
// #string gopher
// #error <nil>ToBase64 encodes the string using standard Base64.
v := str.Of("gopher").ToBase64().String()
godump.Dump(v)
// #string Z29waGVyGoString allows %#v formatting to print the raw string.
v := str.Of("go")
godump.Dump(fmt.Sprintf("%#v", v))
// #string goOf wraps a raw string with fluent helpers.
v := str.Of("gopher")
godump.Dump(v.String())
// #string gopherString returns the underlying raw string value.
v := str.Of("go").String()
godump.Dump(v)
// #string goLen returns the number of runes in the string.
v := str.Of("gophers 🦫").Len()
godump.Dump(v)
// #int 9RuneCount is an alias for Len to make intent explicit in mixed codebases.
v := str.Of("naïve").RuneCount()
godump.Dump(v)
// #int 5Mask replaces the middle of the string with the given rune, revealing revealLeft runes at the start and revealRight runes at the end. Negative reveal values count from the end. If the reveal counts cover the whole string, the original string is returned.
v := str.Of("[email protected]").Mask('*', 3, 4).String()
godump.Dump(v)
// #string gop***********.comIs reports whether the string matches any of the provided wildcard patterns. Patterns use '*' as a wildcard. Case-sensitive.
v := str.Of("foo/bar").Is("foo/*")
godump.Dump(v)
// #bool trueIsMatch reports whether the string matches the provided regular expression.
v := str.Of("abc123").IsMatch(regexp.MustCompile(`\d+`))
godump.Dump(v)
// #bool truePadBoth pads the string on both sides to reach length runes using pad (defaults to space).
v := str.Of("go").PadBoth(6, "-").String()
godump.Dump(v)
// #string --go--PadLeft pads the string on the left to reach length runes using pad (defaults to space).
v := str.Of("go").PadLeft(5, " ").String()
godump.Dump(v)
// #string \u00a0\u00a0\u00a0goPadRight pads the string on the right to reach length runes using pad (defaults to space).
v := str.Of("go").PadRight(5, ".").String()
godump.Dump(v)
// #string go...Remove deletes all occurrences of provided substrings.
v := str.Of("The Go Toolkit").Remove("Go ").String()
godump.Dump(v)
// #string The ToolkitReplaceAll replaces all occurrences of old with new in the string. If old is empty, the original string is returned unchanged.
v := str.Of("go gopher go").ReplaceAll("go", "Go").String()
godump.Dump(v)
// #string Go Gopher GoReplaceArray replaces all occurrences of each old in olds with repl.
v := str.Of("The---Go---Toolkit")
godump.Dump(v.ReplaceArray([]string{"---"}, "-").String())
// #string The-Go-ToolkitReplaceFirst replaces the first occurrence of old with repl.
v := str.Of("gopher gopher").ReplaceFirst("gopher", "go").String()
godump.Dump(v)
// #string go gopherReplaceLast replaces the last occurrence of old with repl.
v := str.Of("gopher gopher").ReplaceLast("gopher", "go").String()
godump.Dump(v)
// #string gopher goReplaceMatches applies repl to each regex match and returns the result.
re := regexp.MustCompile(`\d+`)
v := str.Of("Hello 123 World").ReplaceMatches(re, func(m string) string { return "[" + m + "]" }).String()
godump.Dump(v)
// #string Hello [123] WorldSwap replaces multiple values using strings.Replacer built from a map.
pairs := map[string]string{"Gophers": "GoForj", "are": "is", "great": "fantastic"}
v := str.Of("Gophers are great!").Swap(pairs).String()
godump.Dump(v)
// #string GoForj is fantastic!Contains reports whether the string contains any of the provided substrings (case-sensitive). Empty substrings return true to match strings.Contains semantics.
v := str.Of("Go means gophers").Contains("rust", "gopher")
godump.Dump(v)
// #bool trueContainsAll reports whether the string contains all provided substrings (case-sensitive). Empty substrings are ignored.
v := str.Of("Go means gophers").ContainsAll("Go", "gopher")
godump.Dump(v)
// #bool trueContainsAllFold reports whether the string contains all provided substrings, using Unicode case folding for comparisons. Empty substrings are ignored.
v := str.Of("Go means gophers").ContainsAllFold("go", "GOPHER")
godump.Dump(v)
// #bool trueContainsFold reports whether the string contains any of the provided substrings, using Unicode case folding for comparisons. Empty substrings return true.
v := str.Of("Go means gophers").ContainsFold("GOPHER", "rust")
godump.Dump(v)
// #bool trueCount returns the number of non-overlapping occurrences of sub.
v := str.Of("gogophergo").Count("go")
godump.Dump(v)
// #int 3DoesntContain reports true if none of the substrings are found (case-sensitive). Empty substrings are ignored.
v := str.Of("gophers are great")
godump.Dump(v.DoesntContain("rust", "beam"))
// #bool trueDoesntContainFold reports true if none of the substrings are found using Unicode case folding. Empty substrings are ignored.
v := str.Of("gophers are great")
godump.Dump(v.DoesntContainFold("GOPHER"))
// #bool falseDoesntEndWith reports true if the string ends with none of the provided suffixes (case-sensitive).
v := str.Of("gopher")
godump.Dump(v.DoesntEndWith("her", "cat"))
// #bool falseDoesntEndWithFold reports true if the string ends with none of the provided suffixes using Unicode case folding.
v := str.Of("gopher")
godump.Dump(v.DoesntEndWithFold("HER"))
// #bool falseDoesntStartWith reports true if the string starts with none of the provided prefixes (case-sensitive).
v := str.Of("gopher")
godump.Dump(v.DoesntStartWith("go", "rust"))
// #bool falseDoesntStartWithFold reports true if the string starts with none of the provided prefixes using Unicode case folding.
v := str.Of("gopher")
godump.Dump(v.DoesntStartWithFold("GO"))
// #bool falseEndsWith reports whether the string ends with any of the provided suffixes (case-sensitive).
v := str.Of("gopher").EndsWith("her", "cat")
godump.Dump(v)
// #bool trueEndsWithFold reports whether the string ends with any of the provided suffixes using Unicode case folding.
v := str.Of("gopher").EndsWithFold("HER")
godump.Dump(v)
// #bool trueIndex returns the rune index of the first occurrence of sub, or -1 if not found.
v := str.Of("héllo").Index("llo")
godump.Dump(v)
// #int 2LastIndex returns the rune index of the last occurrence of sub, or -1 if not found.
v := str.Of("go gophers go").LastIndex("go")
godump.Dump(v)
// #int 10StartsWith reports whether the string starts with any of the provided prefixes (case-sensitive).
v := str.Of("gopher").StartsWith("go", "rust")
godump.Dump(v)
// #bool trueStartsWithFold reports whether the string starts with any of the provided prefixes using Unicode case folding.
v := str.Of("gopher").StartsWithFold("GO")
godump.Dump(v)
// #bool trueSlug produces an ASCII slug using the provided separator (default "-"), stripping accents where possible. Not locale-aware; intended for identifiers/URLs.
v := str.Of("Go Forj Toolkit").Slug("-").String()
godump.Dump(v)
// #string go-forj-toolkitExcerpt returns a snippet around the first occurrence of needle with the given radius. If needle is not found, an empty string is returned. If radius <= 0, a default of 100 is used. Omission is used at the start/end when text is trimmed (default "...").
v := str.Of("This is my name").Excerpt("my", 3, "...")
godump.Dump(v.String())
// #string ...is my na...Split splits the string by the given separator.
v := str.Of("a,b,c").Split(",")
godump.Dump(v)
// #[]string [a b c]UcSplit splits the string on uppercase boundaries and delimiters, returning segments.
v := str.Of("HTTPRequestID").UcSplit()
godump.Dump(v)
// #[]string [HTTP Request ID]After returns the substring after the first occurrence of sep. If sep is empty or not found, the original string is returned.
v := str.Of("gopher::go").After("::").String()
godump.Dump(v)
// #string goAfterLast returns the substring after the last occurrence of sep. If sep is empty or not found, the original string is returned.
v := str.Of("pkg/path/file.txt").AfterLast("/").String()
godump.Dump(v)
// #string file.txtBefore returns the substring before the first occurrence of sep. If sep is empty or not found, the original string is returned.
v := str.Of("gopher::go").Before("::").String()
godump.Dump(v)
// #string gopherBeforeLast returns the substring before the last occurrence of sep. If sep is empty or not found, the original string is returned.
v := str.Of("pkg/path/file.txt").BeforeLast("/").String()
godump.Dump(v)
// #string pkg/pathBetween returns the substring between the first occurrence of start and the last occurrence of end. Returns an empty string if either marker is missing or overlapping.
v := str.Of("This is my name").Between("This", "name").String()
godump.Dump(v)
// #string is myBetweenFirst returns the substring between the first occurrence of start and the first occurrence of end after it. Returns an empty string if markers are missing.
v := str.Of("[a] bc [d]").BetweenFirst("[", "]").String()
godump.Dump(v)
// #string aCharAt returns the rune at the given index and true if within bounds.
v, ok := str.Of("gopher").CharAt(2)
godump.Dump(string(v), ok)
// #string p
// #bool trueLimit truncates the string to length runes, appending suffix if truncation occurs.
v := str.Of("Perfectly balanced, as all things should be.").Limit(10, "...").String()
godump.Dump(v)
// #string Perfectly...Slice returns the substring between rune offsets [start:end). Indices are clamped; if start >= end the result is empty.
v := str.Of("naïve café").Slice(3, 7).String()
godump.Dump(v)
// #string e caTake returns the first length runes of the string (clamped).
v := str.Of("gophers").Take(3).String()
godump.Dump(v)
// #string gopTakeLast returns the last length runes of the string (clamped).
v := str.Of("gophers").TakeLast(4).String()
godump.Dump(v)
// #string hersRepeat repeats the string count times (non-negative).
v := str.Of("go").Repeat(3).String()
godump.Dump(v)
// #string gogogoReverse returns a rune-safe reversed string.
v := str.Of("naïve").Reverse().String()
godump.Dump(v)
// #string evïanFirstWord returns the first word token or empty string.
v := str.Of("Hello world")
godump.Dump(v.FirstWord().String())
// #string HelloJoin joins the provided words with sep.
v := str.Of("unused").Join([]string{"foo", "bar"}, "-").String()
godump.Dump(v)
// #string foo-barLastWord returns the last word token or empty string.
v := str.Of("Hello world").LastWord().String()
godump.Dump(v)
// #string worldSplitWords splits the string into words (Unicode letters/digits runs).
v := str.Of("one, two, three").SplitWords()
godump.Dump(v)
// #[]string [one two three]WordCount returns the number of word tokens (letters/digits runs).
v := str.Of("Hello, world!").WordCount()
godump.Dump(v)
// #int 2Words limits the string to count words, appending suffix if truncated.
v := str.Of("Perfectly balanced, as all things should be.").Words(3, " >>>").String()
godump.Dump(v)
// #string Perfectly balanced as >>>WrapWords wraps the string to the given width on word boundaries, using breakStr between lines.
v := str.Of("The quick brown fox jumped over the lazy dog.").WrapWords(20, "\n").String()
godump.Dump(v)
// #string The quick brown fox\njumped over the lazy\ndog.