stringx

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 8 Imported by: 0

README

stringx

Go Reference

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

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 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 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 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