Learn R Programming

exams.forge (version 1.0.11)

affix: Quote, Bracket, and Prefix/Suffix String Manipulation

Description

A set of helper functions for adding or removing specific prefixes and/or suffixes to character vectors. This is useful for formatting strings in mathematical, XML, or other structured text contexts.

  • affix() – Add any prefix and/or suffix to each element of a character vector; alias: add_affix().

  • math() – Add a dollar sign ($) to both ends of each element (LaTeX-style math); aliases: add_math(), lmath().

  • bracket() – Wrap each element in parentheses; aliases: add_bracket(), brkt().

  • cdata() – Wrap each element in <![CDATA[ ... ]]> (XML CDATA section); alias: add_cdata().

  • unaffix() – Remove a given prefix and/or suffix from each element; alias: remove_affix().

  • unquote() – Remove surrounding double quotes from each element; alias: remove_quotes().

  • uncdata() – Remove a surrounding CDATA section from each element.; alias: remove_cdata().

Usage

affix(txt, prefix = "", suffix = "")

math(txt)

bracket(txt)

unaffix(txt, prefix = "", suffix = "")

unquote(txt)

uncdata(txt)

cdata(txt)

add_affix(txt, prefix = "", suffix = "")

add_cdata(txt)

add_math(txt)

lmath(txt)

add_bracket(txt)

brkt(txt)

remove_affix(txt, prefix = "", suffix = "")

remove_quotes(txt)

remove_cdata(txt)

Value

A modified character vector of the same length as txt.

Arguments

txt

character vector to modify.

prefix

character(1) Prefix to add or remove. Default is "".

suffix

character(1) Suffix to add or remove. Default is "".

Examples

Run this code
x <- c("alpha", "beta", "gamma")

# Add a prefix and suffix
affix(x, "[", "]")
#> [1] "[alpha]" "[beta]" "[gamma]"

# Wrap with LaTeX math delimiters
math(x)
#> [1] "$alpha$" "$beta$" "$gamma$"

# Remove quotes
quoted <- c('"a"', '"b"')
unquote(quoted)
#> [1] "a" "b"

# Wrap and unwrap CDATA
cdata_x <- cdata("text")
uncdata(cdata_x)

Run the code above in your browser using DataLab