stringx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 7 Imported by: 0

README

stringx

Go Reference

English | 简体中文

Extended string functions for Go with Unicode support.

Installation

go get github.com/bycigo/stringx

Usage

More examples can be found in the example_test.go.

API Reference

String Functions
Function Description
Len(s string) int Returns the rune count of a string
Reverse(s string) string Reverses a string
PadLeft(s string, length int, padding string) string Pads string on the left
PadRight(s string, length int, padding string) string Pads string on the right
PadBoth(s string, length int, padding string) string Pads string on both sides
Case Conversion Functions
Function Description
CamelCase(s string) string Converts to camelCase
PascalCase(s string) string Converts to PascalCase
SnakeCase(s string) string Converts to snake_case
KebabCase(s string) string Converts to kebab-case
Random String Functions
Function Description
Random(length int) string Generates a random string
Byte Conversion Functions
Function Description
FromBytes(bytes []byte) string Zero-copy conversion from bytes to string
ToBytes(s string) []byte Zero-copy conversion from string to bytes
Password Functions
Function Description
Password(length int, opt ...PasswordOption) string Generates a cryptographically secure password (uses crypto/rand by default)
NewPasswordFactory() PasswordFactory Creates a factory backed by math/rand/v2 (fast, non-cryptographic)
NewSecuredPasswordFactory() PasswordFactory Creates a factory backed by crypto/rand (secure)
NewPasswordFactoryWithSource(src PasswordSource) PasswordFactory Creates a non-cryptographic factory with a custom character source
NewSecuredPasswordFactoryWithSource(src PasswordSource) PasswordFactory Creates a secure factory with a custom character source

Use PasswordIncludes to choose which character kinds (PasswordLetter, PasswordNumber, PasswordSymbol, PasswordSpace) the password may contain. The result always includes at least one character from each selected kind. The default includes letters, numbers and symbols.

License

See LICENSE for details.

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

Examples

Constants

View Source
const (
	// DefaultPasswordLetters is the default set of letters used for generating passwords.
	DefaultPasswordLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	// DefaultPasswordNumbers is the default set of numbers used for generating passwords.
	DefaultPasswordNumbers = "0123456789"
	// DefaultPasswordSymbols is the default set of symbols used for generating passwords.
	DefaultPasswordSymbols = "~!@#$%^&*()-_=+[]{}|;:,.<>?/"
	// DefaultPasswordSpaces is the default set of spaces used for generating passwords.
	DefaultPasswordSpaces = " "
)
View Source
const DefaultRandomAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

DefaultRandomAlphabet is the default alphabet used for generating random strings.

Variables

View Source
var DefaultRandomFactory = NewRandomFactory()

DefaultRandomFactory is the default RandomFactory used for generating random strings.

Functions

func CamelCase

func CamelCase(s string) string

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

func FromBytes(bytes []byte) string

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

func KebabCase(s string) string

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

func Len(s string) int

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

func PadBoth(s string, length int, padding string) string

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

func PadLeft(s string, length int, padding string) string

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

func PadRight(s string, length int, padding string) string

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

func PascalCase(s string) string

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 Password added in v1.1.0

func Password(length int, opt ...PasswordOption) string

Password generates a password of the specified length.

Example
package main

import (
	"fmt"

	"github.com/bycigo/stringx"
)

func main() {
	// Generate a secure password of length 16 using the default character sets
	// (letters, numbers and symbols).
	pwd := stringx.Password(16)
	fmt.Printf("Password length: %d\n", len(pwd))
}
Output:
Password length: 16
Example (Includes)
package main

import (
	"fmt"

	"github.com/bycigo/stringx"
)

func main() {
	// Restrict the password to letters and numbers only.
	pwd := stringx.Password(12, stringx.PasswordIncludes{
		stringx.PasswordLetter,
		stringx.PasswordNumber,
	})
	fmt.Printf("Password length: %d\n", len(pwd))
}
Output:
Password length: 12

func Random

func Random(length int) string

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

func Reverse(s string) string

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

func SnakeCase(s string) string

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

func ToBytes(s string) []byte

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 PasswordCharKind added in v1.1.0

type PasswordCharKind int

PasswordCharKind represents the kind of characters to include in the password.

const (
	PasswordLetter PasswordCharKind
	PasswordNumber
	PasswordSymbol
	PasswordSpace
)

type PasswordFactory added in v1.1.0

type PasswordFactory interface {
	// MakePassword generates a password of the specified length.
	MakePassword(length int, opt ...PasswordOption) string
}

PasswordFactory is an interface for generating passwords.

var DefaultPasswordFactory PasswordFactory = NewSecuredPasswordFactory()

DefaultPasswordFactory is the default PasswordFactory used for generating passwords. It uses crypto/rand for secure random number generation, so the top-level Password function is safe for generating real passwords by default.

func NewPasswordFactory added in v1.1.0

func NewPasswordFactory() PasswordFactory

NewPasswordFactory creates a new PasswordFactory.

Example
package main

import (
	"fmt"

	"github.com/bycigo/stringx"
)

func main() {
	// A non-cryptographic factory is faster and suitable for non-sensitive
	// scenarios such as test fixtures.
	factory := stringx.NewPasswordFactory()
	pwd := factory.MakePassword(20)
	fmt.Printf("Password length: %d\n", len(pwd))
}
Output:
Password length: 20

func NewPasswordFactoryWithSource added in v1.1.0

func NewPasswordFactoryWithSource(src PasswordSource) PasswordFactory

NewPasswordFactoryWithSource creates a new PasswordFactory with the given PasswordSource.

Example
package main

import (
	"fmt"

	"github.com/bycigo/stringx"
)

func main() {
	// Provide a custom character source to fully control the alphabet.
	src := stringx.PasswordSource{
		stringx.PasswordLetter: []byte("abcdef"),
		stringx.PasswordNumber: []byte("0123"),
	}
	factory := stringx.NewPasswordFactoryWithSource(src)
	pwd := factory.MakePassword(16, stringx.PasswordIncludes{
		stringx.PasswordLetter,
		stringx.PasswordNumber,
	})
	fmt.Printf("Password length: %d\n", len(pwd))
}
Output:
Password length: 16

func NewSecuredPasswordFactory added in v1.1.0

func NewSecuredPasswordFactory() PasswordFactory

NewSecuredPasswordFactory creates a new PasswordFactory that uses crypto/rand for secure random number generation.

Example
package main

import (
	"fmt"

	"github.com/bycigo/stringx"
)

func main() {
	// A cryptographically secure factory backed by crypto/rand.
	factory := stringx.NewSecuredPasswordFactory()
	pwd := factory.MakePassword(20)
	fmt.Printf("Password length: %d\n", len(pwd))
}
Output:
Password length: 20

func NewSecuredPasswordFactoryWithSource added in v1.1.0

func NewSecuredPasswordFactoryWithSource(src PasswordSource) PasswordFactory

NewSecuredPasswordFactoryWithSource creates a new PasswordFactory that uses crypto/rand for secure random number generation with the given PasswordSource.

type PasswordIncludes added in v1.1.0

type PasswordIncludes []PasswordCharKind

PasswordIncludes is a PasswordOption that specifies which character kinds to include in the password.

func (PasswordIncludes) ApplyPasswordOptions added in v1.1.0

func (o PasswordIncludes) ApplyPasswordOptions(opts *PasswordOptions)

ApplyPasswordOptions applies the PasswordIncludes option to the given PasswordOptions.

type PasswordOption added in v1.1.0

type PasswordOption interface {
	// ApplyPasswordOptions applies the option to the given PasswordOptions.
	ApplyPasswordOptions(opts *PasswordOptions)
}

PasswordOption is an interface for applying password options.

type PasswordOptions added in v1.1.0

type PasswordOptions struct {
	// Includes specifies which character kinds to include in the password.
	Includes PasswordIncludes
}

PasswordOptions contains options for generating passwords.

func (*PasswordOptions) Join added in v1.1.0

func (o *PasswordOptions) Join(opts ...PasswordOption)

Join applies the given PasswordOption to the PasswordOptions.

type PasswordSource added in v1.1.0

type PasswordSource map[PasswordCharKind][]byte

PasswordSource is a map of character kinds to their corresponding byte slices.

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.

Jump to

Keyboard shortcuts

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