Documentation
¶
Overview ¶
Package strings provides utility functions for string manipulation and processing.
This package offers a collection of helper functions for common string operations including transformations, validations, and conversions that complement the standard library's strings package.
Index ¶
- func Compose(s string, funcs ...func(string) string) string
- func FirstToLower(s string) string
- func FirstToUpper(s string) string
- func HasAnyPrefix(s string, prefixes ...string) bool
- func HasAnySuffix(s string, suffixes ...string) bool
- func IsAlpha(s string) bool
- func IsAlphanumeric(s string) bool
- func IsAnyBlank(strings ...string) bool
- func IsAnyEmpty(s ...string) bool
- func IsBlank(s string) bool
- func IsEmpty(s string) bool
- func IsNumeric(s string) bool
- func IsNumerical(s string) bool
- func PadLeft(s string, n int) string
- func PadRight(s string, n int) string
- func RemoveAll(s string, substrings ...string) string
- func ToCamel(s string) string
- func ToDelimited(s string, delimiter uint8) string
- func ToKebab(s string) string
- func ToLowerCamel(s string) string
- func ToScreamingDelimited(s string, delimiter uint8, ignore string, screaming bool) string
- func ToScreamingKebab(s string) string
- func ToScreamingSnake(s string) string
- func ToSnake(s string) string
- func ToSnakeWithIgnore(s string, ignore string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compose ¶ added in v0.5.0
Compose chains multiple functions to a string.
Example ¶
package main
import (
"fmt"
"strings"
stringsx "github.com/foomo/go/strings"
)
func main() {
result := stringsx.Compose("HELLO", strings.ToLower, stringsx.FirstToUpper)
fmt.Println(result)
}
Output: Hello
func FirstToLower ¶ added in v0.5.0
FirstToLower converts the first character of a string to lowercase.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.FirstToLower("Hello")
fmt.Println(result)
}
Output: hello
func FirstToUpper ¶ added in v0.5.0
FirstToUpper converts the first character of a string to uppercase.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.FirstToUpper("hello")
fmt.Println(result)
}
Output: Hello
func HasAnyPrefix ¶ added in v0.8.0
HasAnyPrefix checks if the given string has any of the provided prefixes and returns true if so.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.HasAnyPrefix("foobar", "foo", "baz"))
}
Output: true
func HasAnySuffix ¶ added in v0.8.0
HasAnySuffix checks if the given string has any of the provided suffixes and returns true if so.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.HasAnySuffix("foobar", "bar", "baz"))
}
Output: true
func IsAlpha ¶ added in v0.8.0
IsAlpha checks if the given string contains only alphabetic characters and returns true if it is, otherwise returns false.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.IsAlpha("abc"))
}
Output: true
func IsAlphanumeric ¶ added in v0.8.0
IsAlphanumeric checks if the given string is alphanumeric and returns true if it is, otherwise returns false.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.IsAlphanumeric("abc1"))
}
Output: true
func IsAnyBlank ¶ added in v0.8.0
IsAnyBlank checks if any of the provided strings in the variadic argument is blank and returns true if so.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.IsAnyBlank("a", " "))
}
Output: true
func IsAnyEmpty ¶ added in v0.8.0
IsAnyEmpty checks if any of the provided strings in the variadic argument is empty and returns true if so.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.IsAnyEmpty("a", ""))
}
Output: true
func IsBlank ¶ added in v0.8.0
IsBlank checks if the given string is blank (contains only whitespace characters) and returns true if it is, otherwise returns false.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.IsBlank(" \t"))
}
Output: true
func IsEmpty ¶ added in v0.8.0
IsEmpty checks if the given string is empty and returns true if it is, otherwise returns false.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.IsEmpty(""))
}
Output: true
func IsNumeric ¶ added in v0.8.0
IsNumeric checks if the given string is numeric and returns true if it is, otherwise returns false.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.IsNumeric("123"))
}
Output: true
func IsNumerical ¶ added in v0.8.0
IsNumerical checks if the given string is numerical and returns true if it is, otherwise returns false.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
fmt.Println(strings.IsNumerical("12.3"))
}
Output: true
func PadLeft ¶ added in v0.2.0
PadLeft prepends spaces to the left of the input string s until it reaches the specified rune length n.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.PadLeft("hello", 10)
fmt.Printf("'%s'", result)
}
Output: ' hello'
func PadRight ¶ added in v0.2.0
PadRight appends spaces to the right of the input string s until it reaches the specified rune length n.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.PadRight("hello", 10)
fmt.Printf("'%s'", result)
}
Output: 'hello '
func RemoveAll ¶ added in v0.2.0
RemoveAll removes all occurrences of each substring from s in a single pass.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.RemoveAll("hello world", "o", "l")
fmt.Println(result)
}
Output: he wrd
func ToCamel ¶
ToCamel converts a string to CamelCase format by removing delimiters and capitalizing appropriate letters.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.ToCamel("hello_world_example")
fmt.Println(result)
}
Output: HelloWorldExample
func ToDelimited ¶
ToDelimited converts a string to delimited.snake.case
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.ToDelimited("HelloWorldExample", '.')
fmt.Println(result)
}
Output: hello.world.example
func ToKebab ¶
ToKebab converts a string to kebab-case format using hyphens as delimiters.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.ToKebab("HelloWorldExample")
fmt.Println(result)
}
Output: hello-world-example
func ToLowerCamel ¶
ToLowerCamel converts a string to lowerCamelCase format by removing delimiters and capitalizing appropriate letters.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.ToLowerCamel("hello_world_example")
fmt.Println(result)
}
Output: helloWorldExample
func ToScreamingDelimited ¶
ToScreamingDelimited converts a string to SCREAMING_DELIMITED_CASE
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.ToScreamingDelimited("HelloWorldExample", '.', "", true)
fmt.Println(result)
}
Output: HELLO.WORLD.EXAMPLE
func ToScreamingKebab ¶
ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE format using hyphens as delimiters.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.ToScreamingKebab("HelloWorldExample")
fmt.Println(result)
}
Output: HELLO-WORLD-EXAMPLE
func ToScreamingSnake ¶
ToScreamingSnake converts a given string to SCREAMING_SNAKE_CASE format using underscores as delimiters.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.ToScreamingSnake("HelloWorldExample")
fmt.Println(result)
}
Output: HELLO_WORLD_EXAMPLE
func ToSnake ¶
ToSnake converts a given string to snake_case format using underscores as delimiters.
Example ¶
package main
import (
"fmt"
"github.com/foomo/go/strings"
)
func main() {
result := strings.ToSnake("HelloWorldExample")
fmt.Println(result)
}
Output: hello_world_example
func ToSnakeWithIgnore ¶
ToSnakeWithIgnore converts the input string `s` to snake_case format while ignoring characters specified in `ignore`.
Types ¶
This section is empty.