commit
33dd14facd
@ -0,0 +1,8 @@
|
|||||||
|
.env
|
||||||
|
.git
|
||||||
|
.svn
|
||||||
|
.idea
|
||||||
|
.vscode
|
||||||
|
*.log
|
||||||
|
gitmod.sh
|
||||||
|
*_test.go
|
@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2018 茂名聚合科技有限公司
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
@ -0,0 +1,25 @@
|
|||||||
|
<h1>
|
||||||
|
<a href="https://www.dtapp.net/">Golang String</a>
|
||||||
|
</h1>
|
||||||
|
|
||||||
|
📦 Golang 字符串组件
|
||||||
|
|
||||||
|
[comment]: <> (go)
|
||||||
|
[![godoc](https://pkg.go.dev/badge/github.com/dtapps/gostring?status.svg)](https://pkg.go.dev/github.com/dtapps/gostring)
|
||||||
|
[![goproxy.cn](https://goproxy.cn/stats/github.com/dtapps/gostring/badges/download-count.svg)](https://goproxy.cn/stats/github.com/dtapps/gostring)
|
||||||
|
[![goreportcard.com](https://goreportcard.com/badge/github.com/dtapps/gostring)](https://goreportcard.com/report/github.com/dtapps/gostring)
|
||||||
|
[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/github.com%2Fdtapps%2Fgostring)
|
||||||
|
|
||||||
|
#### 安装使用
|
||||||
|
|
||||||
|
```go
|
||||||
|
go get -v -u github.com/dtapps/gostring
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 导入
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"github.com/dtapps/gostring"
|
||||||
|
)
|
||||||
|
```
|
@ -0,0 +1,140 @@
|
|||||||
|
package gostring
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/hex"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ToFloat64 string到float64
|
||||||
|
func ToFloat64(s string) float64 {
|
||||||
|
i, _ := strconv.ParseFloat(s, 64)
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToInt string到int
|
||||||
|
func ToInt(s string) int {
|
||||||
|
i, _ := strconv.Atoi(s)
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToInt64 string到int64
|
||||||
|
func ToInt64(s string) int64 {
|
||||||
|
i, err := strconv.ParseInt(s, 10, 64)
|
||||||
|
if err == nil {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
return int64(ToFloat64(s))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToUint string到uint64
|
||||||
|
func ToUint(s string) uint {
|
||||||
|
i, err := strconv.ParseUint(s, 10, 64)
|
||||||
|
if err == nil {
|
||||||
|
return uint(i)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// ToUint64 string到uint64
|
||||||
|
func ToUint64(s string) uint64 {
|
||||||
|
i, err := strconv.ParseUint(s, 10, 64)
|
||||||
|
if err == nil {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace 字符串替换
|
||||||
|
func Replace(str, old, new string) string {
|
||||||
|
return strings.Replace(str, old, new, -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HmacSha256Hex(key, strToSign string) string {
|
||||||
|
hasHer := hmac.New(sha256.New, []byte(key))
|
||||||
|
hasHer.Write([]byte(strToSign))
|
||||||
|
return hex.EncodeToString(hasHer.Sum(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Space 去除空格
|
||||||
|
func Space(str string) string {
|
||||||
|
return strings.Replace(str, " ", "", -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LineBreak 去除换行符
|
||||||
|
func LineBreak(str string) string {
|
||||||
|
return strings.Replace(str, "\n", "", -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SpaceAndLineBreak 去除空格和去除换行符
|
||||||
|
func SpaceAndLineBreak(str string) string {
|
||||||
|
return LineBreak(Space(str))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TrimLastChar 删除字符串中的最后一个
|
||||||
|
func TrimLastChar(s string) string {
|
||||||
|
r, size := utf8.DecodeLastRuneInString(s)
|
||||||
|
if r == utf8.RuneError && (size == 0 || size == 1) {
|
||||||
|
size = 0
|
||||||
|
}
|
||||||
|
return s[:len(s)-size]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Split 字符串分隔
|
||||||
|
func Split(s string, sep string) []string {
|
||||||
|
return strings.Split(s, sep)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Contains 判断字符串是否包含某个字符
|
||||||
|
func Contains(s, substr string) bool {
|
||||||
|
return strings.Contains(s, substr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NumericalToString(value interface{}) (string, bool) {
|
||||||
|
var val string
|
||||||
|
|
||||||
|
switch value.(type) {
|
||||||
|
default:
|
||||||
|
return "0", false
|
||||||
|
case int:
|
||||||
|
intVal, _ := value.(int)
|
||||||
|
val = strconv.FormatInt(int64(intVal), 10)
|
||||||
|
case int8:
|
||||||
|
intVal, _ := value.(int8)
|
||||||
|
val = strconv.FormatInt(int64(intVal), 10)
|
||||||
|
case int16:
|
||||||
|
intVal, _ := value.(int16)
|
||||||
|
val = strconv.FormatInt(int64(intVal), 10)
|
||||||
|
case int32:
|
||||||
|
intVal, _ := value.(int32)
|
||||||
|
val = strconv.FormatInt(int64(intVal), 10)
|
||||||
|
case int64:
|
||||||
|
intVal, _ := value.(int64)
|
||||||
|
val = strconv.FormatInt(int64(intVal), 10)
|
||||||
|
case uint:
|
||||||
|
intVal, _ := value.(uint)
|
||||||
|
val = strconv.FormatUint(uint64(intVal), 10)
|
||||||
|
case uint8:
|
||||||
|
intVal, _ := value.(uint8)
|
||||||
|
val = strconv.FormatUint(uint64(intVal), 10)
|
||||||
|
case uint16:
|
||||||
|
intVal, _ := value.(uint16)
|
||||||
|
val = strconv.FormatUint(uint64(intVal), 10)
|
||||||
|
case uint32:
|
||||||
|
intVal, _ := value.(uint32)
|
||||||
|
val = strconv.FormatUint(uint64(intVal), 10)
|
||||||
|
case uint64:
|
||||||
|
intVal, _ := value.(uint64)
|
||||||
|
val = strconv.FormatUint(intVal, 10)
|
||||||
|
case float32:
|
||||||
|
floatVal, _ := value.(float32)
|
||||||
|
val = strconv.FormatFloat(float64(floatVal), 'f', -1, 32)
|
||||||
|
case float64:
|
||||||
|
floatVal, _ := value.(float64)
|
||||||
|
val = strconv.FormatFloat(floatVal, 'f', -1, 64)
|
||||||
|
}
|
||||||
|
return val, true
|
||||||
|
}
|
Loading…
Reference in new issue