Fluent String Helper
The str package provides a fluent, chainable API for manipulating strings — inspired by Laravel's Str::of(). Every transformation method returns a new NimbusString so you can chain without limits.
Concept
Instead of juggling strings.ToLower(), strings.TrimSpace(), and regexp calls scattered across your code, Nimbus wraps a plain Go string inside NimbusString. You start a chain with str.Str("...") and finish with .String() to get the raw value back. Everything in between is chainable.
import "github.com/CodeSyncr/nimbus/str"
slug := str.Str(" Hello World — Go is Great! ").
Trim().
Slug().
String() // "hello-world-go-is-great"
Available Methods
| Method | Returns | Description |
|---|---|---|
Append(s) | chain | Appends a string to the end |
Prepend(s) | chain | Prepends a string to the beginning |
Upper() | chain | Converts to UPPERCASE |
Lower() | chain | Converts to lowercase |
Title() | chain | Title Case Each Word |
Camel() | chain | camelCase |
Snake() | chain | snake_case |
Kebab() | chain | kebab-case |
Pascal() | chain | PascalCase |
Slug(sep?) | chain | URL-safe slug (default sep -) |
Trim() | chain | Removes leading/trailing whitespace |
Replace(old, new) | chain | Replaces first occurrence |
ReplaceAll(old, new) | chain | Replaces all occurrences |
Limit(n) | chain | Truncate to n characters + “...” |
Words(n) | chain | Truncate to n words + “...” |
Pad(len, pad) | chain | Center-pad to length |
PadLeft(len, pad) | chain | Left-pad to length |
PadRight(len, pad) | chain | Right-pad to length |
Repeat(n) | chain | Repeats the string n times |
Reverse() | chain | Reverses the string |
Mask(char, start, len) | chain | Masks characters (e.g. passwords) |
Excerpt(phrase, radius) | chain | Extract text around a phrase |
Contains(sub) | bool | Checks if the string contains a substring |
StartsWith(prefix) | bool | Checks if the string starts with prefix |
EndsWith(suffix) | bool | Checks if the string ends with suffix |
Length() | int | Returns rune count (Unicode safe) |
WordCount() | int | Returns word count |
IsEmpty() | bool | Returns true for empty string |
Split(sep) | []string | Splits the string by separator |
String() | string | Terminal — returns the raw Go string |
Real-Life Example: Building a URL Slug from User Input
func (ctrl *PostController) Store(c *http.Context) error {
title := c.FormValue("title") // " My Awesome Blog Post! "
post := Post{
Title: str.Str(title).Trim().String(),
Slug: str.Str(title).Trim().Slug().String(), // "my-awesome-blog-post"
}
database.Get().Create(&post)
return c.Redirect(302, "/posts/"+post.Slug)
}
Real-Life Example: Masking Sensitive Data in Logs
func logSafeEmail(email string) string {
// "john.doe@example.com" → "joh****e@example.com"
at := strings.Index(email, "@")
if at <= 2 {
return email
}
local := str.Str(email[:at]).Mask("*", 3, at-4).String()
return local + email[at:]
}
Real-Life Example: Generating API Resource Keys
func resourceKey(modelName string) string {
// "UserProfileSetting" → "user_profile_setting"
return str.Str(modelName).Snake().String()
}
func jsonFieldName(goField string) string {
// "CreatedAt" → "createdAt"
return str.Str(goField).Camel().String()
}
Best Practices
- Always call
.String()at the end of a chain to extract the raw value - Use
Slug()for URL-safe strings,Snake()for database column names - Use
Mask()to redact PII in logs — never log raw emails or tokens - Use
Limit()orWords()for preview text in listings and feeds - Use
Excerpt()to build search result snippets with surrounding context