stringutil

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2025 License: MIT Imports: 2 Imported by: 0

README

String Utilities Package

The stringutil package provides additional string manipulation utilities beyond what's available in the standard library. It offers a collection of helper functions for common string operations that are frequently needed in Go applications.

Features

  • Case-Insensitive Operations: Functions for case-insensitive string comparisons
  • Prefix Checking: Utilities for checking string prefixes with various options
  • String Joining: Advanced string joining with natural language formatting
  • Whitespace Handling: Functions for checking and manipulating whitespace
  • String Truncation: Safely truncate strings with ellipsis
  • Path Normalization: Convert file paths to use consistent separators
  • Regular Expression Utilities: String manipulation using regular expressions

Installation

go get github.com/abitofhelp/servicelib/stringutil

Usage

Case-Insensitive Operations
package main

import (
    "fmt"
    
    "github.com/abitofhelp/servicelib/stringutil"
)

func main() {
    // Check if a string starts with a prefix (case-insensitive)
    hasPrefix := stringutil.HasPrefixIgnoreCase("Hello, World!", "hello")
    fmt.Printf("Has prefix 'hello': %v\n", hasPrefix) // Output: true
    
    // Check if a string contains a substring (case-insensitive)
    contains := stringutil.ContainsIgnoreCase("Hello, World!", "WORLD")
    fmt.Printf("Contains 'WORLD': %v\n", contains) // Output: true
    
    // Convert to lowercase (wrapper around strings.ToLower)
    lower := stringutil.ToLowerCase("Hello, World!")
    fmt.Printf("Lowercase: %s\n", lower) // Output: hello, world!
}
Prefix Checking
package main

import (
    "fmt"
    
    "github.com/abitofhelp/servicelib/stringutil"
)

func main() {
    // Check if a string starts with any of the given prefixes
    hasAnyPrefix := stringutil.HasAnyPrefix("https://example.com", "http://", "https://")
    fmt.Printf("Has any prefix: %v\n", hasAnyPrefix) // Output: true
    
    // Check multiple strings for prefixes
    urls := []string{
        "https://example.com",
        "http://example.org",
        "ftp://example.net",
        "example.io",
    }
    
    for _, url := range urls {
        if stringutil.HasAnyPrefix(url, "http://", "https://") {
            fmt.Printf("%s is a web URL\n", url)
        } else {
            fmt.Printf("%s is not a web URL\n", url)
        }
    }
}
String Joining with Natural Language Formatting
package main

import (
    "fmt"
    
    "github.com/abitofhelp/servicelib/stringutil"
)

func main() {
    // Join strings with "and" (no Oxford comma)
    fruits := []string{"apples", "bananas", "oranges"}
    joined := stringutil.JoinWithAnd(fruits, false)
    fmt.Printf("Without Oxford comma: %s\n", joined) // Output: apples, bananas and oranges
    
    // Join strings with "and" (with Oxford comma)
    joinedOxford := stringutil.JoinWithAnd(fruits, true)
    fmt.Printf("With Oxford comma: %s\n", joinedOxford) // Output: apples, bananas, and oranges
    
    // Handle different list lengths
    fmt.Println(stringutil.JoinWithAnd([]string{}, false))                // Output: ""
    fmt.Println(stringutil.JoinWithAnd([]string{"apples"}, false))        // Output: "apples"
    fmt.Println(stringutil.JoinWithAnd([]string{"apples", "bananas"}, false)) // Output: "apples and bananas"
}
Whitespace Handling
package main

import (
    "fmt"
    
    "github.com/abitofhelp/servicelib/stringutil"
)

func main() {
    // Check if a string is empty or contains only whitespace
    isEmpty := stringutil.IsEmpty("   \t\n   ")
    fmt.Printf("Is empty: %v\n", isEmpty) // Output: true
    
    // Check if a string contains non-whitespace characters
    isNotEmpty := stringutil.IsNotEmpty("  Hello  ")
    fmt.Printf("Is not empty: %v\n", isNotEmpty) // Output: true
    
    // Remove all whitespace from a string
    noWhitespace := stringutil.RemoveWhitespace("Hello, \t World! \n")
    fmt.Printf("No whitespace: %s\n", noWhitespace) // Output: Hello,World!
}
String Truncation
package main

import (
    "fmt"
    
    "github.com/abitofhelp/servicelib/stringutil"
)

func main() {
    // Truncate a long string
    longText := "This is a very long string that needs to be truncated to fit in a limited space."
    truncated := stringutil.Truncate(longText, 20)
    fmt.Printf("Truncated: %s\n", truncated) // Output: This is a very long...
    
    // No truncation needed for short strings
    shortText := "Short text"
    truncatedShort := stringutil.Truncate(shortText, 20)
    fmt.Printf("Truncated short: %s\n", truncatedShort) // Output: Short text
    
    // Handle negative max length
    negativeMax := stringutil.Truncate(longText, -5)
    fmt.Printf("Negative max: %s\n", negativeMax) // Output: ...
}
Path Normalization
package main

import (
    "fmt"
    
    "github.com/abitofhelp/servicelib/stringutil"
)

func main() {
    // Convert Windows-style paths to Unix-style
    windowsPath := "C:\\Users\\username\\Documents\\file.txt"
    unixPath := stringutil.ForwardSlashPath(windowsPath)
    fmt.Printf("Unix path: %s\n", unixPath) // Output: C:/Users/username/Documents/file.txt
    
    // Already Unix-style paths remain unchanged
    alreadyUnix := "/home/username/documents/file.txt"
    normalized := stringutil.ForwardSlashPath(alreadyUnix)
    fmt.Printf("Normalized: %s\n", normalized) // Output: /home/username/documents/file.txt
}

Best Practices

  1. Performance Considerations: For performance-critical code, be aware that some functions like ContainsIgnoreCase create temporary strings.

  2. String Immutability: Remember that strings in Go are immutable, so functions like Truncate and RemoveWhitespace return new strings.

  3. Unicode Support: The functions in this package work with UTF-8 encoded strings, which is Go's default string encoding.

  4. Error Handling: These utility functions don't return errors, so validate input when necessary before calling them.

  5. Path Handling: For more comprehensive path manipulation, consider using the standard library's path/filepath package alongside ForwardSlashPath.

  6. Regular Expressions: RemoveWhitespace uses regular expressions, which can be expensive for very large strings or frequent calls.

  7. String Builders: For complex string manipulations, consider using strings.Builder for better performance.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package stringutil provides additional string manipulation utilities beyond what's available in the standard library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsIgnoreCase

func ContainsIgnoreCase(s, substr string) bool

ContainsIgnoreCase checks if substr is within s, ignoring case. Both s and substr are converted to lowercase before comparison. Parameters:

  • s: The string to search in
  • substr: The substring to search for

Returns:

  • bool: true if substr is found in s (ignoring case), false otherwise

func ForwardSlashPath

func ForwardSlashPath(path string) string

ForwardSlashPath converts backslashes in a path to forward slashes. This is useful for normalizing file paths across different operating systems. Parameters:

  • path: The file path to normalize

Returns:

  • string: The path with all backslashes replaced by forward slashes

func HasAnyPrefix

func HasAnyPrefix(s string, prefixes ...string) bool

HasAnyPrefix checks if the string s begins with any of the specified prefixes. It iterates through the provided prefixes and returns as soon as a match is found. Parameters:

  • s: The string to check
  • prefixes: One or more prefixes to look for at the beginning of s

Returns:

  • bool: true if s starts with any of the prefixes, false otherwise

func HasPrefixIgnoreCase

func HasPrefixIgnoreCase(s, prefix string) bool

HasPrefixIgnoreCase checks if the string s begins with the specified prefix, ignoring case. It is safe for UTF-8 encoded strings and performs a case-insensitive comparison. Parameters:

  • s: The string to check
  • prefix: The prefix to look for at the beginning of s

Returns:

  • bool: true if s starts with prefix (ignoring case), false otherwise

func IsEmpty

func IsEmpty(s string) bool

IsEmpty checks if a string is empty or contains only whitespace. It uses strings.TrimSpace to remove all leading and trailing whitespace. Parameters:

  • s: The string to check

Returns:

  • bool: true if the string is empty or contains only whitespace, false otherwise

func IsNotEmpty

func IsNotEmpty(s string) bool

IsNotEmpty checks if a string is not empty and contains non-whitespace characters. It's the logical opposite of IsEmpty. Parameters:

  • s: The string to check

Returns:

  • bool: true if the string contains non-whitespace characters, false otherwise

func JoinWithAnd

func JoinWithAnd(items []string, useOxfordComma bool) string

JoinWithAnd joins a slice of strings with commas and "and". It handles different list lengths appropriately and supports Oxford comma usage. Parameters:

  • items: The slice of strings to join
  • useOxfordComma: Whether to include a comma before "and" for lists of 3 or more items

Returns:

  • string: The joined string, or an empty string if items is empty

func RemoveWhitespace

func RemoveWhitespace(s string) string

RemoveWhitespace removes all whitespace characters from a string. It uses a regular expression to match and remove spaces, tabs, newlines, and other whitespace. Parameters:

  • s: The string to process

Returns:

  • string: The string with all whitespace characters removed

func ToLowerCase

func ToLowerCase(s string) string

ToLowerCase converts a string to lowercase. It's a simple wrapper around strings.ToLower for consistency within the package. Parameters:

  • s: The string to convert to lowercase

Returns:

  • string: The lowercase version of the input string

func Truncate

func Truncate(s string, maxLength int) string

Truncate truncates a string to a specified maximum length. If the string is longer than maxLength, it adds "..." to the end. If maxLength is negative, it's treated as 0. Parameters:

  • s: The string to truncate
  • maxLength: The maximum length of the returned string before adding "..."

Returns:

  • string: The truncated string, or the original string if it's not longer than maxLength

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL