- LimitUTF8StringLen

This commit is contained in:
gazebo
2019-03-31 12:13:28 +08:00
parent e8c84c3b21
commit ce422881bc
2 changed files with 51 additions and 0 deletions

View File

@@ -246,3 +246,24 @@ func Base64DecodeMultiString(strs ...string) (decodedData [][]byte, err error) {
}
return decodedData, nil
}
func LimitStringLen(str string, maxLen int) (limitedStr string) {
if maxLen > 0 {
if strLen := len(str); strLen > maxLen {
str = str[:maxLen]
}
}
return str
}
func LimitUTF8StringLen(str string, maxLen int) (limitedStr string) {
if maxLen > 0 {
if len(str) > maxLen {
runeList := []rune(str)
if len(runeList) > maxLen {
str = string(runeList[:maxLen])
}
}
}
return str
}