w3soft.org by unix-world
0.00
Left or Right Pad by Length in Go Lang
Helper methods for simple Left or Right Pad a string
programming language: go lang 1.8 or later
operating system: any
Updated: 2022-11-05
Method definition: Left Pad - String
import (
"strings"
)
func LeftPadByLen(s string, padStr string, overallLen int) string {
var padCountInt int = 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr string = strings.Repeat(padStr, padCountInt) + s
return retStr[(len(retStr) - overallLen):]
}
Method definition: Right Pad - String
import (
"strings"
)
func RightPadByLen(s string, padStr string, overallLen int) string {
var padCountInt int = 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr string = s + strings.Repeat(padStr, padCountInt)
return retStr[:overallLen]
}