Skip to content

Commit

Permalink
[feat] pad2str
Browse files Browse the repository at this point in the history
  • Loading branch information
0xVanfer committed Jan 12, 2024
1 parent 7deef6a commit c33ee4c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,24 @@ func StrCamel2Underline(str string) string {
}
return result.String()
}

// Fill the string to a certain length.
//
// Example:
//
// PadToLength("xx", 5, "-") = "xx---"
func PadToLength(str string, length int, fillWith ...string) string {
fill := " "
if len(fillWith) == 1 {
fill = fillWith[0]
}

if len(str) >= length {
return str
}

for len(str) < length {
str += fill
}
return str
}
6 changes: 6 additions & 0 deletions strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ func ExampleStrCamel2Underline() {
// a
// b_a_scccc_s_s
}

func ExamplePadToLength() {
fmt.Println(PadToLength("xx", 5, "-"))
// Output:
// xx---
}

0 comments on commit c33ee4c

Please sign in to comment.