golanghelper/StringUtils.go

36 lines
623 B
Go
Raw Permalink Normal View History

2023-08-18 23:54:06 +08:00
package golanghelper
import "strings"
const (
EMPTY = ""
SPACE = " "
)
type StringUtils struct {
}
// IsEmpty 會檢查 str 是否為空值
func (s *StringUtils) IsEmpty(str string) bool {
return len(str) == 0
}
// LeftPad 將 padStr 以 size 做為長度,向左填滿 str
func (s *StringUtils) LeftPad(str string, size int, padStr string) string {
if s.IsEmpty(padStr) {
padStr = SPACE
}
padStrLen := len(padStr)
padCount := size - len(str)
if padCount <= 0 || padCount < padStrLen {
return str
}
if padCount == padStrLen {
return padStr + str
}
return strings.Repeat(padStr, padCount) + str
}