Created
December 20, 2018 05:51
-
-
Save mchampaneri/679a64bb8340869c9a2841dbd72e9849 to your computer and use it in GitHub Desktop.
string padding helper functions
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 main | |
import "fmt" | |
func PadRight(str, pad string, lenght int) string { | |
for { | |
str += pad | |
if len(str) > lenght { | |
return str[0:lenght] | |
} | |
} | |
} | |
func PadLeft(str, pad string, lenght int) string { | |
for { | |
str = pad + str | |
if len(str) > lenght { | |
return str[1:lenght+1] | |
} | |
} | |
} | |
func main() { | |
str := fmt.Sprintf("%d",123) | |
fmt.Println(PadRight(str, "x", 5)) // expects abcxx | |
fmt.Println(PadLeft(str, "x", 5)) // expects xxabc | |
fmt.Println(PadRight(str, "xyz", 5)) // expects abcxy | |
fmt.Println(PadLeft(str, "xyz", 5)) // expects xyzab | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment