1 Star 0 Fork 0

szmaozi/go-utils

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
stringutil.go 3.88 KB
Copy Edit Raw Blame History
szmaozi authored 2021-09-09 20:00 . wip:
/*
* @Name:
* @Descripttion:
* @Warning:
* @version:
* @Author: moo
* @Date: 2020-12-26 17:58:06
* @LastEditors: moo
* @LastEditTime: 2021-03-26 16:05:47
*/
// stringutil 包含有用于处理字符串的工具函数。
package utils
import (
"crypto/md5"
"encoding/hex"
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/gobuffalo/packr/v2"
"golang.org/x/net/html/charset"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode"
"golang.org/x/text/transform"
)
//获取URL的GET参数
func GetUrlArg(r *http.Request, name string) string {
var arg string
values := r.URL.Query()
arg = values.Get(name)
return arg
}
// Reverse 将其实参字符串以符文为单位左右反转。
func Reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
// Reverse2 将其实参字符串以符文为单位左右反转。
func Reverse2(s string) string {
r := []rune(s)
left := 0
right := len(r) - 1
for left < right {
r[left], r[right] = r[right], r[left]
left++
right--
}
return string(r)
}
//转换编码
func Encoding(html string, ct string) string {
e, name := DetermineEncoding(html)
if name != "utf-8" {
html = ConvertToStr(html, "gbk", "utf-8")
e = unicode.UTF8
}
r := strings.NewReader(html)
utf8Reader := transform.NewReader(r, e.NewDecoder())
//将其他编码的reader转换为常用的utf8reader
all, _ := ioutil.ReadAll(utf8Reader)
return string(all)
}
func DetermineEncoding(html string) (encoding.Encoding, string) {
e, name, _ := charset.DetermineEncoding([]byte(html), "")
return e, name
}
//获取文件内容,可以打包到二进制
func FileGetContent(file string) string {
str := ""
box := packr.New("tmpl", "../static")
content, err := box.FindString(file)
if err != nil {
return str
}
return content
}
//md5加密
func Md5(src string) string {
m := md5.New()
m.Write([]byte(src))
res := hex.EncodeToString(m.Sum(nil))
return res
}
func ShowStringByte(str string) {
s := []byte(str)
for i, c := range s {
fmt.Println(i, c)
}
}
// string to int
func StrToInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
return -1
}
return i
}
func StrToUint(s string) uint {
i, err := strconv.Atoi(s)
if err != nil {
return uint(0)
}
return uint(i)
}
func IsNumber(s string) bool {
m, _ := regexp.MatchString("^[0-9]+$", s)
if m {
return true
} else {
return false
}
}
func IsEmail(s string) bool {
m, _ := regexp.MatchString(`^([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`, s)
if m {
return true
} else {
return false
}
}
func IsMobile(s string) bool {
m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`, s)
if m {
return true
} else {
return false
}
}
// 中国身份证
func IsIDCard(s string) bool {
//验证15位身份证,15位的是全部数字
m, _ := regexp.MatchString(`^(\d{15})$`, s)
if m {
return true
} else {
//验证18位身份证,18位前17位为数字,最后一位是校验位,可能为数字或字符X
m, _ = regexp.MatchString(`^(\d{17})([0-9]|X)$`, s)
if m {
return true
} else {
return false
}
}
}
func IsDate(s string) bool {
_, err := String2Time(s)
if err != nil {
return false
} else {
return true
}
}
func String2Time(in string) (out time.Time, err error) {
in = strings.Replace(in, "/", "-", -1)
fmt.Println(in)
if len(in) > 10 {
out, err = time.Parse("2006-01-02 15:04:05", in) //layout使用"2006/01/02 15:04:05"此数据格式转换会出错
} else {
out, err = time.Parse("2006-01-02", in) //layout使用"2006/01/02"此数据格式转换会出错
}
return out, err
}
func IsHan(s string) bool {
m, _ := regexp.MatchString("^\\p{Han}+$", s)
if m {
return true
} else {
return false
}
}
func IsEnChar(s string) bool {
m, _ := regexp.MatchString("^[a-zA-Z]+$", s)
if m {
return true
} else {
return false
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/szmaozi/go-utils.git
git@gitee.com:szmaozi/go-utils.git
szmaozi
go-utils
go-utils
29e02a007caf

Search