Documentation
¶
Overview ¶
Package stringx provides extended string functions for Go with Unicode support.
This package extends the standard library's strings package with additional functionality for common string operations.
Index ¶
- Constants
- Variables
- func CamelCase(s string) string
- func FromBytes(bytes []byte) string
- func KebabCase(s string) string
- func Len(s string) int
- func PadBoth(s string, length int, padding string) string
- func PadLeft(s string, length int, padding string) string
- func PadRight(s string, length int, padding string) string
- func PascalCase(s string) string
- func Random(length int) string
- func Reverse(s string) string
- func SnakeCase(s string) string
- func ToBytes(s string) []byte
- type RandomFactory
- type RandomOption
- type RandomOptions
Examples ¶
Constants ¶
const DefaultRandomAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
DefaultRandomAlphabet is the default alphabet used for generating random strings.
Variables ¶
var DefaultRandomFactory = NewRandomFactory()
DefaultRandomFactory is the default RandomFactory used for generating random strings.
Functions ¶
func CamelCase ¶
CamelCase converts a string to camel case.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.CamelCase("hello_world"))
fmt.Println(stringx.CamelCase("hello-world"))
fmt.Println(stringx.CamelCase("hello world"))
fmt.Println(stringx.CamelCase("HelloWorld"))
}
Output: helloWorld helloWorld helloWorld helloWorld
func FromBytes ¶
FromBytes converts a byte slice to a string without copying the data. This is more efficient than using string(bytes) because it avoids unnecessary memory allocation. However, be cautious when using this function, as any modification to the bytes will also modify the resulting string, since they share the same underlying data. Use this function in appropriate scenarios where you can guarantee that the byte slice will not be modified after conversion.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
bytes := []byte("hello world")
s := stringx.FromBytes(bytes)
fmt.Println(s)
}
Output: hello world
func KebabCase ¶
KebabCase converts a string to kebab case.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.KebabCase("helloWorld"))
fmt.Println(stringx.KebabCase("HelloWorld"))
fmt.Println(stringx.KebabCase("hello_world"))
fmt.Println(stringx.KebabCase("HTTPServer"))
}
Output: hello-world hello-world hello-world http-server
func Len ¶
Len returns the actual character length of the string, not the byte length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.Len("hello"))
fmt.Println(stringx.Len("你好"))
fmt.Println(stringx.Len("🎉🎊"))
}
Output: 5 2 2
func PadBoth ¶
PadBoth pads the string on both sides with the specified padding string until it reaches the desired length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.PadBoth("go", 6, "x"))
fmt.Println(stringx.PadBoth("hi", 8, "ab"))
fmt.Println(stringx.PadBoth("test", 10, "="))
}
Output: xxgoxx abahiaba ===test===
func PadLeft ¶
PadLeft pads the string on the left with the specified padding string until it reaches the desired length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.PadLeft("go", 5, "x"))
fmt.Println(stringx.PadLeft("hello", 10, "0"))
fmt.Println(stringx.PadLeft("test", 8, "ab"))
}
Output: xxxgo 00000hello ababtest
func PadRight ¶
PadRight pads the string on the right with the specified padding string until it reaches the desired length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.PadRight("go", 5, "x"))
fmt.Println(stringx.PadRight("hello", 10, "0"))
fmt.Println(stringx.PadRight("test", 8, "ab"))
}
Output: goxxx hello00000 testabab
func PascalCase ¶
PascalCase converts a string to pascal case.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.PascalCase("hello_world"))
fmt.Println(stringx.PascalCase("hello-world"))
fmt.Println(stringx.PascalCase("hello world"))
fmt.Println(stringx.PascalCase("helloWorld"))
}
Output: HelloWorld HelloWorld HelloWorld HelloWorld
func Random ¶
Random generates a random string of the specified length.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
// Generate a random string of length 10
s := stringx.Random(10)
fmt.Printf("Random string length: %d\n", len(s))
}
Output: Random string length: 10
func Reverse ¶
Reverse reverses the string.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.Reverse("hello"))
fmt.Println(stringx.Reverse("你好世界"))
fmt.Println(stringx.Reverse("abc123"))
}
Output: olleh 界世好你 321cba
func SnakeCase ¶
SnakeCase converts a string to snake case.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
fmt.Println(stringx.SnakeCase("helloWorld"))
fmt.Println(stringx.SnakeCase("HelloWorld"))
fmt.Println(stringx.SnakeCase("hello-world"))
fmt.Println(stringx.SnakeCase("HTTPServer"))
}
Output: hello_world hello_world hello_world http_server
func ToBytes ¶
ToBytes converts a string to a byte slice without copying the data. This is more efficient than using []byte(s) because it avoids unnecessary memory allocation. However, be cautious when using this function, as any modification to the resulting byte slice will cause a panic, since strings are immutable in Go. Use this function in appropriate scenarios where you can guarantee that the resulting byte slice will not be modified after conversion.
Example ¶
package main
import (
"fmt"
"github.com/bycigo/stringx"
)
func main() {
s := "hello world"
bytes := stringx.ToBytes(s)
fmt.Println(string(bytes))
}
Output: hello world
Types ¶
type RandomFactory ¶
type RandomFactory interface {
// MakeRandomString generates a random string of the specified length.
MakeRandomString(length int) string
}
RandomFactory is an interface for generating random strings.
func NewRandomFactory ¶
func NewRandomFactory(opt ...RandomOption) RandomFactory
NewRandomFactory creates a new RandomFactory.
type RandomOption ¶
type RandomOption func(opts *RandomOptions)
RandomOption is a function that modifies RandomOptions.
func RandomAlphabet ¶
func RandomAlphabet(alphabet string) RandomOption
RandomAlphabet sets the alphabet for generating random strings.
type RandomOptions ¶
type RandomOptions struct {
// Alphabet is the set of characters to use for generating random strings.
Alphabet []rune
}
RandomOptions contains options for generating random strings.