commit 35a7205d1c551cfd8d514f11caf2e1d6c7bfe685 Author: dennis Date: Fri Aug 18 23:54:06 2023 +0800 Initial commit diff --git a/StringUtils.go b/StringUtils.go new file mode 100644 index 0000000..07bde77 --- /dev/null +++ b/StringUtils.go @@ -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 +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5e43740 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitlab-ce.niaulang.com/niau-lang/golanghelper + +go 1.20