xstrings

package
v0.0.0-...-f89cf20 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2020 License: MIT Imports: 9 Imported by: 0

README

XStrings

标准库对string的操作比较有限,所有诞生了该库

Enchanced join

package main

import (
    "fmt"

    "github.com/leaxoy/x-go/xcodec"
    _ "github.com/leaxoy/x-go/xcodec/json"
    "github.com/leaxoy/x-go/xstrings"
)

type intStringer int

func (i intStringer) String() string {
    return fmt.Sprintf("%d", i*i)
}

func main() {
    xstrings.JoinInt([]int{1, 2, 3}, "#") // Output: 1#2#3
    xstrings.JoinInt8([]int8{1, 2, 3}, "#") // Output: 1#2#3
    xstrings.JoinInt16([]int16{1, 2, 3}, "#") // Output: 1#2#3
    xstrings.JoinInt32([]int32{1, 2, 3}, "#") // Output: 1#2#3
    xstrings.JoinInt64([]int64{1, 2, 3}, "#") // Output: 1#2#3

    xstrings.JoinUint([]uint{1, 2, 3}, "#") // Output: 1#2#3
    xstrings.JoinUint8([]uint8{1, 2, 3}, "#") // Output: 1#2#3
    xstrings.JoinUint16([]uint16{1, 2, 3}, "#") // Output: 1#2#3
    xstrings.JoinUint32([]uint32{1, 2, 3}, "#") // Output: 1#2#3
    xstrings.JoinUint64([]uint64{1, 2, 3}, "#") // Output: 1#2#3
    xstrings.JoinStringer([]fmt.Stringer{intStringer(1), intStringer(2), intStringer(3)}, "#") // Output: 1#4#9
    xstrings.JoinAny([]interface{}{}, "#", xcodec.Get("json"))
}

Random string

package main

import (
    "github.com/leaxoy/x-go/xstrings"
)

func main() {
    xstrings.Random(xstrings.RandomSet, 32) // 生成长度为32的随机字符串,字符来自xstrings.RandomSet
    xstrings.FastRandom(32) // 生成长度为32的随机字符串
}

Enhanced split

package main

import (
    "github.com/leaxoy/x-go/xstrings"
)

func main() {
    xstrings.SplitInt("1$2$3", "$") // Output: [1, 2, 3], nil
    xstrings.SplitInt8("1$2$3", "$") // Output: [1, 2, 3], nil
    xstrings.SplitInt16("1$2$3", "$") // Output: [1, 2, 3], nil
    xstrings.SplitInt32("1$2$3", "$") // Output: [1, 2, 3], nil
    xstrings.SplitInt64("1$2$3", "$") // Output: [1, 2, 3], nil

    xstrings.SplitUint("1$2$3", "$") // Output: [1, 2, 3], nil
    xstrings.SplitUint8("1$2$3", "$") // Output: [1, 2, 3], nil
    xstrings.SplitUint16("1$2$3", "$") // Output: [1, 2, 3], nil
    xstrings.SplitUint32("1$2$3", "$") // Output: [1, 2, 3], nil
    xstrings.SplitUint64("1$2$3", "$") // Output: [1, 2, 3], nil
}

Transform

package main

import (
    "github.com/leaxoy/x-go/xstrings"
)

func main() {
    xstrings.Reverse("Hello") // Output: olleH
    xstrings.CamelCase("hello") // Output: Hello
    xstrings.SnakeCase("HelloWorld") // Output: hello_world
    xstrings.Delete("Hello", "l") // Output: Heo
    xstrings.Count("Hello", "l") // Output: 2
}

Documentation

Index

Constants

View Source
const RandomSet = "0123456789ABCDEF"

Variables

This section is empty.

Functions

func CamelCase

func CamelCase(str string) string

CamelCast transform a string to camel case

func Count

func Count(str string, pattern string) int

Count count `pattern` appear times in `str`

func Delete

func Delete(str string, pattern string) string

Delete delete all `pattern` in `str`

func FastRandom

func FastRandom(n int) (string, error)

FastRandom creates new string with one read, this version is fast than the random version.

func JoinAny

func JoinAny(i []interface{}, sep string, codec xcodec.Codec) (string, error)

func JoinInt

func JoinInt(i []int, sep string) string

func JoinInt16

func JoinInt16(i []int16, sep string) string

func JoinInt32

func JoinInt32(i []int32, sep string) string

func JoinInt64

func JoinInt64(i []int64, sep string) string

func JoinInt8

func JoinInt8(i []int8, sep string) string

func JoinStringer

func JoinStringer(i []fmt.Stringer, sep string) string

func JoinUint

func JoinUint(i []uint, sep string) string

func JoinUint16

func JoinUint16(i []uint16, sep string) string

func JoinUint32

func JoinUint32(i []uint32, sep string) string

func JoinUint64

func JoinUint64(i []uint64, sep string) string

func JoinUint8

func JoinUint8(i []uint8, sep string) string

func KebabCase

func KebabCase(str string) string

KebabCast transform a string to kebab case

func Random

func Random(strSet string, length int) (string, error)

Random creates new string based on strSet. It uses crypto/rand as the random number generator. error is the one returned by rand.Int

func Reverse

func Reverse(str string) string

Reverse reverse str rune by rune

func SnakeCase

func SnakeCase(str string) string

SnakeCast transform a string to snake case

func SplitInt

func SplitInt(str string, sep string) ([]int, error)

func SplitInt16

func SplitInt16(str string, sep string) ([]int16, error)

func SplitInt32

func SplitInt32(str string, sep string) ([]int32, error)

func SplitInt64

func SplitInt64(str string, sep string) ([]int64, error)

func SplitInt8

func SplitInt8(str string, sep string) ([]int8, error)

func SplitUint

func SplitUint(str string, sep string) ([]uint, error)

func SplitUint16

func SplitUint16(str string, sep string) ([]uint16, error)

func SplitUint32

func SplitUint32(str string, sep string) ([]uint32, error)

func SplitUint64

func SplitUint64(str string, sep string) ([]uint64, error)

func SplitUint8

func SplitUint8(str string, sep string) ([]uint8, error)

func UnsafeBytesToString

func UnsafeBytesToString(data []byte) string

func UnsafeStringToBytes

func UnsafeStringToBytes(str string) []byte

Types

This section is empty.

Jump to

Keyboard shortcuts

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