Vectorised over string
, width
and pad
.
str_pad(string, width, side = c("left", "right", "both"), pad = " ")
string | A character vector. |
---|---|
width | Minimum width of padded strings. |
side | Side on which padding character is added (left, right or both). |
pad | Single padding character (default is a space). |
A character vector.
str_trim()
to remove whitespace;
str_trunc()
to decrease the maximum width of a string.
rbind( str_pad("hadley", 30, "left"), str_pad("hadley", 30, "right"), str_pad("hadley", 30, "both") )#> [,1] #> [1,] " hadley" #> [2,] "hadley " #> [3,] " hadley "#> [1] " a" " abc" " abcdef"#> [1] " a" " a" " a"#> [1] "---------a" "_________a" " a"# Longer strings are returned unchanged str_pad("hadley", 3)#> [1] "hadley"