Str
A fluent, Laravel-inspired string toolkit for Go with explicit, rune-safe helpers and predictable behavior.
Install / Use
/learn @goforj/StrREADME
Installation
go get github.com/goforj/str
Runnable examples
Every 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.
<!-- api:embed:start -->API Index
| Group | Functions | |------:|-----------| | Affixes | ChopEnd ChopStart EnsurePrefix EnsureSuffix HasSurrounding Unwrap Wrap | | Case | Camel Headline Kebab LcFirst Pascal Snake Title ToLower ToTitle ToUpper UcFirst UcWords | | Checks | IsASCII IsAlnum IsAlpha IsBlank IsEmpty IsNumeric | | Cleanup | Deduplicate NormalizeNewlines NormalizeSpace Squish Trim TrimLeft TrimRight TrimSpace | | Comparison | Equals EqualsFold | | Compose | Append NewLine Prepend | | Constructor | Of | | Conversion | Bool Float64 Int | | Encoding | FromBase64 ToBase64 | | Fluent | GoString String | | Length | Len RuneCount | | Masking | Mask | | Match | Is IsMatch Match MatchAll | | Padding | PadBoth PadLeft PadRight | | Pluralize | Plural Singular | | Replace | Remove ReplaceAll ReplaceArray ReplaceEnd ReplaceFirst ReplaceFirstFold ReplaceFold ReplaceLast ReplaceLastFold ReplaceMatches ReplaceStart Swap | | Search | Contains ContainsAll ContainsAllFold ContainsFold Count CountFold EndsWith EndsWithFold Index IndexFold LastIndex LastIndexFold StartsWith StartsWithFold | | Slug | Slug | | Snippet | Excerpt | | Split | Lines Split UcSplit | | Substrings | After AfterFold AfterLast AfterLastFold Before BeforeFold BeforeLast BeforeLastFold Between BetweenFirst CharAt CommonPrefix CommonSuffix Limit Slice SubstrReplace Take TakeLast | | Transform | Repeat Reverse Transliterate | | Words | FirstWord Initials Join LastWord SplitWords WordCount Words WrapWords |
Affixes
<a id="chopend"></a>ChopEnd
ChopEnd removes the first matching suffix if present.
v := str.Of("file.txt").ChopEnd(".txt", ".md").String()
println(v)
// #string file
<a id="chopstart"></a>ChopStart
ChopStart removes the first matching prefix if present.
v := str.Of("https://goforj.dev").ChopStart("https://", "http://").String()
println(v)
// #string goforj.dev
<a id="ensureprefix"></a>EnsurePrefix
EnsurePrefix ensures the string starts with prefix, adding it if missing.
v := str.Of("path/to").EnsurePrefix("/").String()
println(v)
// #string /path/to
<a id="ensuresuffix"></a>EnsureSuffix
EnsureSuffix ensures the string ends with suffix, adding it if missing.
v := str.Of("path/to").EnsureSuffix("/").String()
println(v)
// #string path/to/
<a id="hassurrounding"></a>HasSurrounding
HasSurrounding reports whether the string starts with before and ends with after. If after is empty, before is used for both sides.
v := str.Of(`"GoForj"`).HasSurrounding(`"`, "")
println(v)
// #bool true
<a id="unwrap"></a>Unwrap
Unwrap removes matching before and after strings if present.
v := str.Of(`"GoForj"`).Unwrap(`"`, `"`).String()
println(v)
// #string GoForj
<a id="wrap"></a>Wrap
Wrap surrounds the string with before and after (after defaults to before).
v := str.Of("GoForj").Wrap(`"`, "").String()
println(v)
// #string "GoForj"
Case
<a id="camel"></a>Camel
Camel converts the string to camelCase.
v := str.Of("foo_bar baz").Camel().String()
println(v)
// #string fooBarBaz
<a id="headline"></a>Headline
Headline 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()
println(v)
// #string Email Notification Sent
<a id="kebab"></a>Kebab
Kebab converts the string to kebab-case.
v := str.Of("fooBar baz").Kebab().String()
println(v)
// #string foo-bar-baz
<a id="lcfirst"></a>LcFirst
LcFirst returns the string with the first rune lower-cased.
v := str.Of("Gopher").LcFirst().String()
fmt.Println(v)
// #string gopher
<a id="pascal"></a>Pascal
Pascal converts the string to PascalCase.
v := str.Of("foo_bar baz").Pascal().String()
fmt.Println(v)
// #string FooBarBaz
<a id="snake"></a>Snake
Snake converts the string to snake_case using the provided separator (default "_").
v := str.Of("fooBar baz").Snake("_").String()
println(v)
// #string foo_bar_baz
<a id="title"></a>Title
Title 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()
println(v)
// #string A Nice Title Uses The Correct Case
<a id="tolower"></a>ToLower
ToLower returns a lowercase copy of the string using Unicode rules.
v := str.Of("GoLang").ToLower().String()
println(v)
// #string golang
<a id="totitle"></a>ToTitle
ToTitle returns a title-cased copy where all letters are mapped using Unicode title case.
v := str.Of("ß").ToTitle().String()
println(v)
// #string SS
<a id="toupper"></a>ToUpper
ToUpper returns an uppercase copy of the string using Unicode rules.
v := str.Of("GoLang").ToUpper().String()
println(v)
// #string GOLANG
<a id="ucfirst"></a>UcFirst
UcFirst returns the string with the first rune upper-cased.
v := str.Of("gopher").UcFirst().String()
println(v)
// #string Gopher
<a id="ucwords"></a>UcWords
UcWords uppercases the first rune of each word, leaving the rest unchanged. Words are sequences of letters/digits.
v := str.Of("hello WORLD").UcWords().String()
println(v)
// #string Hello WORLD
Checks
<a id="isascii"></a>IsASCII
IsASCII reports whether the string consists solely of 7-bit ASCII runes.
v := str.Of("gopher").IsASCII()
println(v)
// #bool true
<a id="isalnum"></a>IsAlnum
IsAlnum reports whether the string contains at least one rune and every rune is a Unicode letter or number.
v := str.Of("Gopher2025").IsAlnum()
println(v)
// #bool true
<a id="isalpha"></a>IsAlpha
IsAlpha reports whether the string contains at least one rune and every rune is a Unicode letter.
v := str.Of("Gopher").IsAlpha()
println(v)
// #bool true
<a id="isblank"></a>IsBlank
IsBlank reports whether the string contains only Unicode whitespace.
v := str.Of(" \\t\\n")
println(v.IsBlank())
// #bool true
<a id="isempty"></a>IsEmpty
IsEmpty reports whether the string has zero length.
v := str.Of("").IsEmpty()
println(v)
// #bool true
<a id="isnumeric"></a>IsNumeric
IsNumeric reports whether the string contains at least one rune and
