Created
June 15, 2021 22:37
-
-
Save felipevolpatto/9e7704afc488d812b7cefddbd420d9c0 to your computer and use it in GitHub Desktop.
Pad in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package strutil | |
// PadLeft left pads a string str with "pad". The string is padded to | |
// the size of width. | |
func PadLeft(str string, width int, pad string) string { | |
return Tile(pad, width-Len(str)) + str | |
} | |
// PadRight right pads a string str with "pad". The string is padded to | |
// the size of width. | |
func PadRight(str string, width int, pad string) string { | |
return str + Tile(pad, width-Len(str)) | |
} | |
// Pad left and right pads a string str with leftPad and rightPad. The string | |
// is padded to the size of width. | |
func Pad(str string, width int, leftPad string, rightPad string) string { | |
switch { | |
case Len(leftPad) == 0: | |
return PadRight(str, width, rightPad) | |
case Len(rightPad) == 0: | |
return PadLeft(str, width, leftPad) | |
} | |
padLen := (width - Len(str)) / 2 | |
return Tile(leftPad, padLen) + str + Tile(rightPad, width-Len(str)-padLen) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment