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:


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]

}