Initial commit
This commit is contained in:
commit
35a7205d1c
35
StringUtils.go
Normal file
35
StringUtils.go
Normal file
@ -0,0 +1,35 @@
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue
Block a user