str_pad: Pad a string.

Description Usage Arguments Value See Also Examples

Description

Vectorised over string, width and pad.

Usage

1
str_pad(string, width, side = c("left", "right", "both"), pad = " ")

Arguments

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).

Value

A character vector.

See Also

str_trim() to remove whitespace; str_trunc() to decrease the maximum width of a string.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
rbind(
  str_pad("hadley", 30, "left"),
  str_pad("hadley", 30, "right"),
  str_pad("hadley", 30, "both")
)

# All arguments are vectorised except side
str_pad(c("a", "abc", "abcdef"), 10)
str_pad("a", c(5, 10, 20))
str_pad("a", 10, pad = c("-", "_", " "))

# Longer strings are returned unchanged
str_pad("hadley", 3)

Example output

     [,1]                            
[1,] "                        hadley"
[2,] "hadley                        "
[3,] "            hadley            "
[1] "         a" "       abc" "    abcdef"
[1] "    a"                "         a"           "                   a"
[1] "---------a" "_________a" "         a"
[1] "hadley"

stringr documentation built on May 2, 2019, 11:03 a.m.