str_prefix: Add prefix or suffix to a string

Description Usage Arguments Details Value See Also Examples

View source: R/str_prefix.R

Description

Adds a prefix or suffix to strings

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
str_prefix(x, prefix, ...)

str_postfix(x, postfix, ...)

str_suffix(x, postfix, ...)

str_prefix_idem(x, prefix, ...)

str_postfix_idem(x, postfix, ...)

str_suffix_idem(x, postfix, ...)

str_unprefix(string, prefix, ...)

str_unpostfix(string, postfix, ...)

str_unsuffix(string, postfix, ...)

Arguments

x

character vector or object with a names attribute

prefix, postfix

string for pre- or postfix

...

additional arguments passed to subsequent functions

Details

This is a convenient wrapper for paste0( x, prefix, ...) and paste( x, suffix, ...).

str_postfix is an alias for str_postfix.

str_prefix_idem() and str_postfix_idem() are idempotent forms of str_prefix() and str_postfix(). Pre- and postfixes are not added to elements that are already pre- or postfixed.

str_unprefix and str_unpostfix/str_unsuffix removes pre- and postfixes. With these, the prefix and postfix are taken as regular expression, so that one function call can remove a variety of pre- and postfixes.

str_unpostfix and str_unsuffix removes postfix (if it exists) from the string.

Value

character vector with each element of x prefixed or sufffixed as specified.

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  lets <- letters[1:5]
  str_prefix( lets, "pre." )
  str_posfix( lets, ".post" )

 x <- c( "foo", "pre.foo", "bar", "pre.bar", "one.two.three" )
 str_prefix_idem(x, "pre.")

 x <- c( "foo", "foo.post", "bar", "bar.post", "one.two.three" )
 str_postfix_idem(x, ".post")

 # Idempotency
 str_postfix_idem( str_postfix_idem(x, ".post"), ".post" )

  lets <- letters[1:5]
  prefixed <- str_prefix( lets, "pre." )
  str_unprefix( prefixed, "pre." )

  lets <- letters[1:5]
  postfixed <- str_postfix( lets, "pre." )
  str_unpostfix( postfixed, "pre." )

decisionpatterns/stringr.tools documentation built on Nov. 4, 2019, 10:24 a.m.