str_arithmetic: String Arithmetic Operators

str_arithmeticR Documentation

String Arithmetic Operators

Description

String arithmetic operators.

The x %s+% y operator is exported from 'stringi', and concatenates character vectors x and y.

The x %s-% p operator removes character/pattern defined in p from x.

The x %s*% n operator is exported from 'stringi', and duplicates each string in x n times, and concatenates the results.

The x %s/% p operator counts how often character/pattern defined in p occurs in each element of x.

The x %s//% brk operator counts how often the text boundary specified in list brk occurs in each element of x.

The e1 %s$% e2 operator is exported from 'stringi', and provides access to stri_sprintf in the form of an infix operator.

The x %ss% p operator splits the strings in x by a delimiter character/pattern defined in p, and removes p in the process.
For cutting strings by text boundaries, or around a location, see strcut_brk and strcut_loc.

Usage

x %s-% p

x %s/% p

x %s//% brk

x %ss% p

Arguments

x

a string or character vector.

p

either a list with 'stringi' arguments (see s_pattern), or else a character vector with regular expressions.
[REGEX]
[FIXED]
[COLL]
[CHARCLASS]

brk

a list with break iteration options, like a list produced by stri_opts_brkiter.
[BOUNDARIES]

Value

The %s+%, %s-%, and %s*% operators return a character vector of the same length as x.
The %s/% and %s//% both return an integer vector of the same length as x.
The %s$% operator returns a character vector.
The %ss% operator returns a list of the split strings - or, if simplify = TRUE / simplify = NA, returns a matrix of the split strings.

See Also

tinycodet_strings

Examples


x <- c(paste0(letters[1:13], collapse = ""),
       paste0(letters[14:26], collapse = ""))
print(x)
y <- c("a", "b")
p <- rep("a|e|i|o|u", 2) # same as p <- list(regex = rep("a|e|i|o|u", 2))
n <- c(3, 2)

x %s+% y # = paste0(x,y)
x %s-% p # remove all vowels from x
x %s*% n
x %s/% p # count how often vowels appear in each string of vector x
x %ss% p # split x around vowels, removing the vowels in the process
x %ss% s_regex(p, simplify = NA) # same as above, but in matrix form

test <- c(
  paste0("The\u00a0above-mentioned    features are very useful. ",
  "Spam, spam, eggs, bacon, and spam. 123 456 789"),
  "good morning, good evening, and good night"
)
test %s//% list(type = "character")


x <- c(paste0(letters[1:13], collapse = ""),
       paste0(letters[14:26], collapse = ""))
print(x)
y <- "a"
# pattern that ignores case:
p <- list(regex = rep("A|E|I|O|U", 2), case_insensitive = TRUE)
n <- c(2, 3)

x %s+% y # = paste0(x,y)
x %s-% p # remove all vowels from x
x %s*% n
x %s/% p # count how often vowels appears in each string of vector x.

x <- c(paste(letters, collapse = ", "), paste(LETTERS, collapse = ", "))
print(x)
x %ss% ", "
t(x %ss% s_fixed(", ", simplify = NA))


tinycodet documentation built on Sept. 12, 2024, 7:03 a.m.