github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xstrings/README.md (about)

     1  # XStrings
     2  
     3  标准库对string的操作比较有限,所有诞生了该库
     4  
     5  ## Enchanced join
     6  
     7  ``` go
     8  package main
     9  
    10  import (
    11      "fmt"
    12  
    13      "github.com/go-board/x-go/xcodec"
    14      _ "github.com/go-board/x-go/xcodec/json"
    15      "github.com/go-board/x-go/xstrings"
    16  )
    17  
    18  type intStringer int
    19  
    20  func (i intStringer) String() string {
    21      return fmt.Sprintf("%d", i*i)
    22  }
    23  
    24  func main() {
    25      xstrings.JoinInt([]int{1, 2, 3}, "#") // Output: 1#2#3
    26      xstrings.JoinInt8([]int8{1, 2, 3}, "#") // Output: 1#2#3
    27      xstrings.JoinInt16([]int16{1, 2, 3}, "#") // Output: 1#2#3
    28      xstrings.JoinInt32([]int32{1, 2, 3}, "#") // Output: 1#2#3
    29      xstrings.JoinInt64([]int64{1, 2, 3}, "#") // Output: 1#2#3
    30  
    31      xstrings.JoinUint([]uint{1, 2, 3}, "#") // Output: 1#2#3
    32      xstrings.JoinUint8([]uint8{1, 2, 3}, "#") // Output: 1#2#3
    33      xstrings.JoinUint16([]uint16{1, 2, 3}, "#") // Output: 1#2#3
    34      xstrings.JoinUint32([]uint32{1, 2, 3}, "#") // Output: 1#2#3
    35      xstrings.JoinUint64([]uint64{1, 2, 3}, "#") // Output: 1#2#3
    36      xstrings.JoinStringer([]fmt.Stringer{intStringer(1), intStringer(2), intStringer(3)}, "#") // Output: 1#4#9
    37      xstrings.JoinAny([]interface{}{}, "#", xcodec.Get("json"))
    38  }
    39  ```
    40  
    41  ## Random string
    42  
    43  ``` go
    44  package main
    45  
    46  import (
    47      "github.com/go-board/x-go/xstrings"
    48  )
    49  
    50  func main() {
    51      xstrings.Random(xstrings.RandomSet, 32) // 生成长度为32的随机字符串,字符来自xstrings.RandomSet
    52      xstrings.FastRandom(32) // 生成长度为32的随机字符串
    53  }
    54  ```
    55  
    56  ## Enhanced split
    57  
    58  ``` go
    59  package main
    60  
    61  import (
    62      "github.com/go-board/x-go/xstrings"
    63  )
    64  
    65  func main() {
    66      xstrings.SplitInt("1$2$3", "$") // Output: [1, 2, 3], nil
    67      xstrings.SplitInt8("1$2$3", "$") // Output: [1, 2, 3], nil
    68      xstrings.SplitInt16("1$2$3", "$") // Output: [1, 2, 3], nil
    69      xstrings.SplitInt32("1$2$3", "$") // Output: [1, 2, 3], nil
    70      xstrings.SplitInt64("1$2$3", "$") // Output: [1, 2, 3], nil
    71  
    72      xstrings.SplitUint("1$2$3", "$") // Output: [1, 2, 3], nil
    73      xstrings.SplitUint8("1$2$3", "$") // Output: [1, 2, 3], nil
    74      xstrings.SplitUint16("1$2$3", "$") // Output: [1, 2, 3], nil
    75      xstrings.SplitUint32("1$2$3", "$") // Output: [1, 2, 3], nil
    76      xstrings.SplitUint64("1$2$3", "$") // Output: [1, 2, 3], nil
    77  }
    78  ```
    79  
    80  ## Transform
    81  
    82  ``` go
    83  package main
    84  
    85  import (
    86      "github.com/go-board/x-go/xstrings"
    87  )
    88  
    89  func main() {
    90      xstrings.Reverse("Hello") // Output: olleH
    91      xstrings.CamelCase("hello") // Output: Hello
    92      xstrings.SnakeCase("HelloWorld") // Output: hello_world
    93      xstrings.Delete("Hello", "l") // Output: Heo
    94      xstrings.Count("Hello", "l") // Output: 2
    95  }
    96  ```