Longest common prefix/suffix

trim_common_affixes(x, .x = NULL, na.rm = TRUE, prefixes = TRUE,
  suffixes = TRUE, warn_if_no_prefix = TRUE,
  warn_if_no_suffix = TRUE)

longest_suffix(x, .x = NULL, na.rm = TRUE, warn_if_no_suffix = TRUE)

longest_prefix(x, .x = NULL, na.rm = TRUE, warn_if_no_prefix = TRUE)

Arguments

x

A character vector.

.x

If NULL, the default, ignored. May be used if x is known to be free of NAs.

na.rm

(logical, default: TRUE) If FALSE, an NA in x means "" is the only common affix. If NA, the longest prefix/suffix is NA_character_ (provided anyNA(x)).

If anyNA(x) == FALSE na.rm has no effect.

prefixes

(logical, default: TRUE) If TRUE, trim prefixes.

suffixes

(logical, default: TRUE) If TRUE, trim suffixes.

warn_if_no_prefix, warn_if_no_suffix

(logical, default: TRUE) If FALSE, if x has no common affixes the warning is suppressed. (If no common prefix/suffix then the common affix returned will be "" (the empty string).)

Value

The longest common substring in x either at the start or end of each string. For trim_common_affixes x with common prefix and common suffix removed.

Examples

longest_prefix(c("totalx", "totaly", "totalz"))
#> [1] "total"
longest_suffix(c("ztotal", "ytotal", "xtotal"))
#> [1] "total"