stringutil

package
v1.4.0 Latest Latest
Warning

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

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

README

Stringutil Module

The Stringutil Module 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

Quick Start

See the Case-Insensitive Operations example for a complete, runnable example of how to use the Stringutil module.

API Documentation

Case-Insensitive Operations

The module provides functions for case-insensitive string operations like prefix checking and substring searching.

Case-Insensitive String Operations

See the Case-Insensitive Operations example for a complete, runnable example of how to use case-insensitive string operations.

Prefix Checking

The HasAnyPrefix function checks if a string starts with any of the specified prefixes.

Checking String Prefixes

See the Prefix Checking example for a complete, runnable example of how to check string prefixes.

String Joining with Natural Language Formatting

The JoinWithAnd function joins a slice of strings with commas and "and", with optional Oxford comma support.

Joining Strings with Natural Language Formatting

See the String Joining example for a complete, runnable example of how to join strings with natural language formatting.

Whitespace Handling

The module provides functions for checking if a string is empty or contains only whitespace, and for removing whitespace.

Handling Whitespace in Strings

See the Whitespace Handling example for a complete, runnable example of how to handle whitespace in strings.

String Truncation

The Truncate function truncates a string to a specified maximum length, adding an ellipsis if needed.

Truncating Strings

See the String Truncation example for a complete, runnable example of how to truncate strings.

Path Normalization

The ForwardSlashPath function converts backslashes in a path to forward slashes for consistent path handling across platforms.

Normalizing File Paths

See the Path Normalization example for a complete, runnable example of how to normalize file paths.

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