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

MethodReturnsDescription
Append(s)chainAppends a string to the end
Prepend(s)chainPrepends a string to the beginning
Upper()chainConverts to UPPERCASE
Lower()chainConverts to lowercase
Title()chainTitle Case Each Word
Camel()chaincamelCase
Snake()chainsnake_case
Kebab()chainkebab-case
Pascal()chainPascalCase
Slug(sep?)chainURL-safe slug (default sep -)
Trim()chainRemoves leading/trailing whitespace
Replace(old, new)chainReplaces first occurrence
ReplaceAll(old, new)chainReplaces all occurrences
Limit(n)chainTruncate to n characters + “...”
Words(n)chainTruncate to n words + “...”
Pad(len, pad)chainCenter-pad to length
PadLeft(len, pad)chainLeft-pad to length
PadRight(len, pad)chainRight-pad to length
Repeat(n)chainRepeats the string n times
Reverse()chainReverses the string
Mask(char, start, len)chainMasks characters (e.g. passwords)
Excerpt(phrase, radius)chainExtract text around a phrase
Contains(sub)boolChecks if the string contains a substring
StartsWith(prefix)boolChecks if the string starts with prefix
EndsWith(suffix)boolChecks if the string ends with suffix
Length()intReturns rune count (Unicode safe)
WordCount()intReturns word count
IsEmpty()boolReturns true for empty string
Split(sep)[]stringSplits the string by separator
String()stringTerminal — 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() or Words() for preview text in listings and feeds
  • Use Excerpt() to build search result snippets with surrounding context