36 lines
623 B
Go
36 lines
623 B
Go
|
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
|
||
|
}
|