第一版string2other

This commit is contained in:
白茶清欢 2023-05-05 10:59:24 +08:00
parent 6741dcf033
commit 96532aff00
1 changed files with 258 additions and 0 deletions

View File

@ -393,3 +393,261 @@ func (sc *stringConvert) ToIntSlice(str string, splitChar ...string) ([]int, err
}
return res, nil
}
// ToUint8Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:50 2023/5/5
func (sc *stringConvert) ToUint8Slice(str string, splitChar ...string) ([]uint8, error) {
var (
err error
res []uint8
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint8, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint8(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint16Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:52 2023/5/5
func (sc *stringConvert) ToUint16Slice(str string, splitChar ...string) ([]uint16, error) {
var (
err error
res []uint16
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint16, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint16(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:53 2023/5/5
func (sc *stringConvert) ToUint32Slice(str string, splitChar ...string) ([]uint32, error) {
var (
err error
res []uint32
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint32, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint32(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
func (sc *stringConvert) ToUint64Slice(str string, splitChar ...string) ([]uint64, error) {
var (
err error
res []uint64
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint64, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint64(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUintSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
func (sc *stringConvert) ToUintSlice(str string, splitChar ...string) ([]uint, error) {
var (
err error
res []uint
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToUint(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToBoolSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:55 2023/5/5
func (sc *stringConvert) ToBoolSlice(str string, splitChar ...string) ([]bool, error) {
var (
err error
res []bool
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]bool, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToBool(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToFloat32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
func (sc *stringConvert) ToFloat32Slice(str string, splitChar ...string) ([]float32, error) {
var (
err error
res []float32
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]float32, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToFloat32(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToFloat64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
func (sc *stringConvert) ToFloat64Slice(str string, splitChar ...string) ([]float64, error) {
var (
err error
res []float64
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]float64, 0)
arr := strings.Split(str, splitChar[0])
for _, item := range arr {
if itemVal, err := sc.ToFloat64(item); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToDoubleSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
func (sc *stringConvert) ToDoubleSlice(str string, splitChar ...string) ([]float64, error) {
return sc.ToFloat64Slice(str, splitChar...)
}
// ToNumberSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
func (sc *stringConvert) ToNumberSlice(str string, splitChar ...string) ([]float64, error) {
return sc.ToFloat64Slice(str, splitChar...)
}