diff --git a/str.go b/str.go index 72c8661..6e097b2 100644 --- a/str.go +++ b/str.go @@ -3,6 +3,8 @@ package str import ( + "encoding/json" + "fmt" "math/rand" "strings" "time" @@ -89,14 +91,24 @@ func MapsKeysString(imap interface{}) string { 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) -//// } -//// } +// string json to string map to string +func JsonstrToStrMap(jsonstr string) (m map[string]string) { + err := json.Unmarshal([]byte(jsonstr), &m) + if err != nil { + panic(fmt.Sprintf("Jsonstr[%s] To MapString failed:%s", jsonstr, err.Error())) + } + return m +} + +// string to string map to string +// example: "K1:V1;K2:V2" +// => {K1:V1, K2:V2} +func StringToStrMap(str string, sep1, sep2 string) (m map[string]string) { + m = map[string]string{} + for _, kvstr := range strings.Split(str, sep1) { + if kv := strings.SplitN(kvstr, sep2, 2); len(kv) == 2 { + m[kv[0]] = kv[1] + } + } + return m +}