diff --git a/strutil/string.go b/strutil/string.go index 80bcfa9..8b54475 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -67,6 +67,13 @@ func LowerFirst(s string) string { return string(r) + s[size:] } +// PadStart pads string on the left and right side if it's shorter than size. +// Padding characters are truncated if they exceed size. +// Play: todo +func Pad(source string, size int, padStr string) string { + return padAtPosition(source, size, padStr, 0) +} + // PadStart pads string on the left side if it's shorter than size. // Padding characters are truncated if they exceed size. // Play: https://go.dev/play/p/xpTfzArDfvT diff --git a/strutil/string_test.go b/strutil/string_test.go index 6682ebc..6edc4d0 100644 --- a/strutil/string_test.go +++ b/strutil/string_test.go @@ -168,6 +168,15 @@ func TestLowerFirst(t *testing.T) { } } +func TestPad(t *testing.T) { + assert := internal.NewAssert(t, "TestPad") + + assert.Equal("a ", Pad("a", 2, "")) + assert.Equal("a", Pad("a", 1, "b")) + assert.Equal("ab", Pad("a", 2, "b")) + assert.Equal("mabcdm", Pad("abcd", 6, "m")) +} + func TestPadEnd(t *testing.T) { assert := internal.NewAssert(t, "TestPadEnd")