From 099a33cccfe6fbe40219083fe29db848b92d5dc6 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 12 Oct 2022 20:52:27 +0800 Subject: [PATCH] init version --- README.md | 3 ++ go.mod | 3 ++ str.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 str.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..7ce8ec6 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# personal str functions + + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..50ac5bd --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module qoobing.com/gomod/str + +go 1.16 diff --git a/str.go b/str.go new file mode 100644 index 0000000..9bbebab --- /dev/null +++ b/str.go @@ -0,0 +1,111 @@ +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. +package str + +import ( + "container/list" + "encoding/json" + "errors" + "fmt" + "math/rand" + "os" + "regexp" + "runtime" + "strings" + "syscall" + "time" +) + +const ( + NUMBERSTRING = "0123456789" + CHARSTRING = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" +) + +// get random number string with length. +func GetRandomString(l int) string { + str := NUMBERSTRING + bytes := []byte(str) + result := []byte{} + r := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := 0; i < l; i++ { + result = append(result, bytes[r.Intn(len(bytes))]) + } + return string(result) +} + +// get random number+alphabate string with length. +func GetRandomCharString(l int) string { + str := CHARSTRING + bytes := []byte(str) + result := []byte{} + r := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := 0; i < l; i++ { + result = append(result, bytes[r.Intn(len(bytes))]) + } + return string(result) +} + +//skip +func Skip(s string, sep string, n int) string { + seplen := len(sep) + if seplen == 0 || n < 0 { + return s + } + + for { + i := strings.Index(s, sep) + if i < 0 { + return s + } else { + s = s[i+seplen:] + n = n - 1 + if n <= 0 { + return s + } + } + } + return s +} + +// skip line +func SkipLine(s string, n int) string { + return Skip(s, "\n", n) +} + +// get a map keys as array. +func MapKeys(imap interface{}) (keys []string) { + switch imap.(type) { + case map[string]string: + m := imap.(map[string]string) + for k, _ := range m { + keys = append(keys, k) + } + case map[string]interface{}: + m := imap.(map[string]interface{}) + for k, _ := range m { + keys = append(keys, k) + } + default: + panic("Unkown Type") + } + return keys +} + +// get a map keys and join to string. +func KeysString(imap interface{}) string { + keys := Keys(imap) + return strings.Join(keys, ",") +} + + +//// CHARSTRING string = "" //will initialize by init function +//// func init() { +//// NUMBERSTRING = "0123456789" +//// CHARSTRING = NUMBERSTRING +//// for i := 'a'; i <= 'z'; i++ { +//// CHARSTRING = CHARSTRING + string(i) +//// } +//// for i := 'A'; i <= 'Z'; i++ { +//// CHARSTRING = CHARSTRING + string(i) +//// } +//// }